c++ - spoj prime conjecture PRIMEZUK -


i trying solve question http://www.spoj.com/problems/primezuk/

 #include<iostream>  #include<cstdio> #include<math.h> #define l long long  using namespace std; l chk(l a) {     for(int i=2;i<=sqrt(a);++i)     {         if(a%i==0)         {             return a/i;         }     }     return 0; } main() { //  freopen("in.txt","r",stdin);     int t,n,a;     l prod=1,flag;     //t=inp();     cin>>t;     for(int j=1;j<=t;++j)     {         cin>>n;         //n=inp();         if(n==0)         prod=-1;         else         prod=1;         while(n--)         {             cin>>a;             //a=inp();             prod*=a;         }         ++prod;         flag=chk(prod);         if(!flag)         printf("case #%d: %lld\n",j,prod);         else         printf("case #%d: %lld\n",j,flag);     } } 

i getting right answer sample test case whne submit getting wrong answer...any hints???

you getting wrong answer because "return a/i" may return non-prime number.so should check whether "return a/i" prime or not..

try this...

#include<bits/stdc++.h>  long long int check(long long int a)//function check whether number prime or not {     long long int i,k;     k=sqrt(a);     for(i=2;i<=k;++i)     {         if(a%i==0) // if not prime             return check(a/i); //then find greatest prime     }     return a; }  int main() {     int j=1,t;     long long int n,a,prod,flag;     scanf("%d",&t);     while(t--)     {         scanf("%lld",&n);         if(n==0)             prod=-1;         else              prod=1;         while(n--)         {             scanf("%lld",&a);             prod*=a;         }         ++prod;         printf("case #%d: %lld\n",j,check(prod));         j++;     }     return 0; }    

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -