-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
23-SeongHoonC #186
23-SeongHoonC #186
Conversation
์๊ฐ ๋ณต์ก๋ ๊ณ์ฐ ๋ง์ํ์๋ ๊ฑธ๊น์์ ?! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def generatePrime(maximum):
if maximum <= 1:
return None
prime_numbers = []
for number in range(2,maximum+1):
for i in range(2,int(number**0.5)+1):
if number%i ==0:
break
else:
prime_numbers.append(number)
return prime_numbers
target = int(input())
if target <= 1:
print("0")
else:
primeNumbers = generatePrime(target)
#print(primeNumbers)
left, right = 0,0
temp = 2
count = 0
while left<=right:
if temp < target:
right += 1
if right >= len(primeNumbers): break
temp += primeNumbers[right]
elif temp > target:
temp -= primeNumbers[left]
left += 1
else:
right += 1
count += 1
if right >= len(primeNumbers): break
temp += primeNumbers[right]
print(count)
์ผ๋จ ์ฌ๋ ค ๋๊ตฌ ๋ฆฌ๋ทฐ๋ ๋์ฆ์ ํคํค
๋์ ํฉ๋ ์ธ ํ์๊ฐ ์์๊ตฐ์ ํฉ ๋ณ์๋ฅผ ๋ง๋ค๊ณ head tail ์ ๋ณ์์ ๋ํด์ฃผ๋ฉด ๋๋๊ฑฐ์๋ค์ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๊ฑฐ๋ ๋๊ฐ์ ๋ก์ง์ผ๋ก C++ ์ฝ๋ ์ ์ถํด๋ดค๋๋ฐ ์ฝํ๋ฆฐ์ด๋ ์๊ฐ ์ฐจ์ด๊ฐ ์๋๋ค์...?
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
bool IsPrime(int n)
{
for(int i = 2; i * i <= n; ++i)
{
if(n % i == 0)
{
return false;
}
}
return true;
}
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int N; cin >> N;
vector<int> Primes;
for(int i = 2; i <= N; ++i)
{
if(IsPrime(i))
{
Primes.emplace_back(i);
}
}
int Head = 0, Tail = 0;
int Answer = 0, Sum = 0;
while(Head < Primes.size() && Tail < Primes.size())
{
int Sum = accumulate(&Primes[Head], &Primes[Tail + 1], 0);
if(Sum < N)
{
Tail++;
continue;
}
if(Sum == N)
{
Answer++;
}
Head++;
}
cout << Answer;
return 0;
}
์๊ฐํด๋ณด๋ 2 ~ 4,000,000 ์ซ์์ ๋ํด ๋ค ์์๋ฅผ ํ๋ณํ๋ค๋ณด๋ ์๊ฐ์ด ์ค๋๊ฑธ๋ฆด ์ ๋ฐ์ ์๋ค์... ์ฌ๊ธฐ์ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฝ๋๋ค.
์๋ผํ ์คํ ๋ค์ค์ ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์๊ฐ์ด ๋ํญ ์ค์ด๋ญ๋๋ค. ์ ๊ฒฝ์ฐ์ 20ms ๊น์ง ์ค์ด๋๋ค์ ใ ใ
์์ ๋ฌธ์ ๋ฅผ ๋ณด๋ฉด ๋งค๋ฒ ์๊ฐ๋๋ ์๋ผํ ์คํ ๋ค์ค์ ์ฒด์ธ๋ฐ ์ธํฐ๋ท์ ์ฐธ๊ณ ํ๊ณ ์ด๋ป๊ฒ ๊ตฌํํ๋์ง ์ดํด๋ ํ๋๋ฐ ๋ค์ ํ๋ ค๋ฉด ์๋๋๋ผ๊ตฌ์... ์๊ฐ์ด ๋ ๋ ๋ธ๋ก๊ทธ์ ์ ๋ฆฌ๋ฅผ ํ๋ฒ ํด์ผํ ๊ฒ ๊ฐ๋ค์!!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ ๋ณด๊ณ ๋ฐ๋ก ์๋ผํ ์คํ
๋ค์ค์ ์ฒด?! ํ๊ณ ์ ๊ทผํ์ต๋๋ค
๊ทผ๋ฐ ๊ตฌํ์ ๊ธฐ์ต์ด ์๋์ ๊ณต๋ถํ๊ณ ๋ค์ ์ ๊ทผํ์ต๋๋ค ใ
;; ๋๋ถ์ ๋ณต์ตํ์ต๋๋ค (_ _)
ํ์.. ๊ทผ๋ฐ ์ฐ์๋ ์์์ ํฉ์ ์ฒ๋ฆฌํ๋ ๊ณผ์ ์์ ์ซ ์ค๋ ๊ฑธ๋ ธ๋ค์
์ฑํ๋ PR์ head, tail ๋ถ๋ถ ๋ก์ง ์ง๋ค๊ฐ (์ ์ฝ๋์ while ๋ฌธ) IndexError๊ฐ ์๊พธ ๋ฐ์ํด์ ๊ฒจ์ฐ ์ฒ๋ฆฌํ๋ค์ฉ ..
๋ฌธ์ ํธ๋๋ผ ์๊ณ ๋ง์ผ์ จ์ด๋ค ~~ ๐๐
import sys
import math
def input(): return sys.stdin.readline().rstrip()
N = int(input())
if N <= 1:
print("0")
sys.exit(0)
total = 0
result = 0
start = 0
end = 0
arr = [True for i in range(N+1)]
prime_arr = []
for i in range(2, int(math.sqrt(N)) + 1):
if arr[i]:
for j in range(i*i, N+1, i):
arr[j] = False
for i in range(2, len(arr)):
if arr[i] == True:
prime_arr.append(i)
total = prime_arr[start]
while start<=end and end<=len(prime_arr):
if total < N:
if 0<end<len(prime_arr):
total += prime_arr[end]
end += 1
elif total == N:
result += 1
total -= prime_arr[start]
start += 1
else:
total -= prime_arr[start]
start += 1
print(result)
๐ ๋ฌธ์ ๋งํฌ
์์์ ์ฐ์ํฉ
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
โจ ์๋ ์ฝ๋
์์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด์ head tail ์ฎ๊ฒจ๊ฐ๋ฉด์ ํด๊ฒฐํ๋๊ฑด ์ฝ๊ฒ ๋ ์ฌ๋์ต๋๋ค.
๊ทธ๋ฌ๋ ์๊ฐ ์ด๊ณผ์ ๋ด์ฐฉ...
์๊ฐ๋ ์๊ฐ ์ด๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ
์๋ผํ ์คํ ๋ค์ค๋ ๋ฌธ์ ํ ๋ ๋ฐ๋ก ์๊ฐ ์๋์ ๊ทธ๋ฅ ์ ๊ณฑ๊ทผ๊น์ง ํ์์ผ๋ก ์ฐ์ ํ์์ต๋๋น.. ๊ทธ๋ฌ๋๋ ํด๊ฒฐ!
๋์ ํฉ์ ์ ์ฉํ๋ ์ํ๋ ์๊ฐ ์ฐจ์ด๊ฐ ์ผ๋ง์๋๋ค์...
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์๊ฐ ์ด๊ณผ๋ฅผ ์ด๋ป๊ฒ ๊ณ์ฐํ๋์ง ์์ง ์๋ชฐ๋ผ์ ํน์ ์ด ๋ฌธ์ ์์ ์๊ฐ์ด๊ณผ ๊ณ์ฐ ๋ฐฉ๋ฒ์ ์๋ ค์ฃผ์ค ์ ์๋์?