Uva 1210 - Sum of Consecutive Prime Numbers
/*
Do Cumulative Sum among Prime Numbers till 10000. Count the Sum and Print it with Constant Complexity :)
*/
/*
Verdict : Accepted
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
#include<bitset>
#define N 10005
#define psize 1300
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
bitset<N>bs;
LL prm[psize],plen=1;
int ans[N];
void sieve()
{
LL i,j;
bs.set();
bs[0] = bs[1] = 0;
LL sum=0;
for(i=2; i<N; i++)
{
if(bs[i])
{
prm[plen++] = i;
ans[i]+=1;
for(j=i*i;j<N;j+=i)
{
bs[j] = 0;
}
}
}
for(i=2; i<plen; i++)
{
sum = prm[i-1];
for(j=i; j<=plen; j++)
{
sum += prm[j];
if(sum <= N)
{
ans[sum]+=1;
}
else
{
break;
}
}
}
}
int main()
{
sieve();
int n;
while(~sf("%d",&n) and n)
{
pf("%d\n",ans[n]);
}
return 0;
}
Do Cumulative Sum among Prime Numbers till 10000. Count the Sum and Print it with Constant Complexity :)
*/
/*
Verdict : Accepted
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
#include<bitset>
#define N 10005
#define psize 1300
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef vector<LL>vll;
bitset<N>bs;
LL prm[psize],plen=1;
int ans[N];
void sieve()
{
LL i,j;
bs.set();
bs[0] = bs[1] = 0;
LL sum=0;
for(i=2; i<N; i++)
{
if(bs[i])
{
prm[plen++] = i;
ans[i]+=1;
for(j=i*i;j<N;j+=i)
{
bs[j] = 0;
}
}
}
for(i=2; i<plen; i++)
{
sum = prm[i-1];
for(j=i; j<=plen; j++)
{
sum += prm[j];
if(sum <= N)
{
ans[sum]+=1;
}
else
{
break;
}
}
}
}
int main()
{
sieve();
int n;
while(~sf("%d",&n) and n)
{
pf("%d\n",ans[n]);
}
return 0;
}
Comments
Post a Comment