Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, May 22, 2014

aps-Amazing Prime Sequence

Amazing Prime Sequence

given below c code for aps spoj or amazing prime sequence.
If you have any quarry you can mail me @ raj.nishant360@gmail.com
#include <bits/stdc++.h>
int p[10000009];
long long res[10000009];
void pre()
{
 for(int i=2;i<=10000000;i++)
 {
  if(!p[i])
  {
   for(int j=i+i;j<=10000000;j+=i)
    if(!p[j])
     p[j]=i;
   res[i]=res[i-1]+i;
  }
  else
   res[i]=res[i-1]+p[i];
 }
}
int main()
{
 int t;
 pre();
 scanf("%d",&t);
 while(t--)
 {
  int n;
  scanf("%d",&n);
  printf("%lld\n",res[n]);
 }
 return 0;
}

1 comment:

  1. #include
    using namespace std;
    int p[100];
    int res[100];
    int prime(int n){
    res[0]=0;
    res[1]=0;
    p[0]=0;
    p[1]=1;
    for(int i=2;i<=n;i++){
    if(p[i]==0){
    p[i]=i;
    //res[i]=res[i-1]+i;
    int j=2;
    while(i*j<=n){
    if(p[i*j]==0){
    p[i*j]=i;
    res[i]=res[i-1]+i;
    }
    j++;
    }


    }
    else{
    res[i]=res[i-1]+p[i];
    }

    }
    return res[n];
    }
    int main() {
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    cout<<prime(n)<<endl;
    }
    }



    please tell why it is not working in third test case when n=4 it is giving output 9.
    please help

    ReplyDelete

Your comment is valuable to us