Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

RE的好厉害,帮忙看一下吧

Posted by ecjtubaowp at 2007-01-15 13:46:16 on Problem 2844
#include<stdio.h>
#include<math.h>
__int64 x[1000001];
__int64 sum[1000001],fact[1000001];
int prime[10000],p0[100000000],xp[100000000];
__int64 n,s,p,total,count,total1;
bool isprime(int n)
 {int i;int m;
 if(n==1)return 0;
 m=(int)sqrt((double)n);
 for(i=2;i<=m;i++)
 if(n%i==0)break;
 if(i>m)return 1;
 else return 0;
 }
 void fenjie(int n)
 {
  int i,j,k,m;
  m=(int)sqrt((double)n);
  for(i=2;i<=n;i++)
  {
   if(n%i==0){p0[total1]=i;total1++;}
  }
 }
 void recur(int v0)
 {
  int i,j,k;
  for(x[v0]=0;x[v0]<total;x[v0]++)
  {
   sum[v0]=sum[v0-1]+xp[x[v0]];
   fact[v0]=fact[v0-1]*xp[x[v0]];
   if(count>=1)break;
   if(v0<n)recur(v0+1);
   else
   {
    if(sum[v0]==s&&fact[v0]==p)
    {
     count++;
     for(i=1;i<=v0;i++)
     printf("%d ",xp[x[i]]);
     printf("\n");
     
   }//if
  }//else
 }//for
}
int main()
{
 int i,j,k,m;
 int sum0;
 while(scanf("%I64d%I64d%I64d",&n,&s,&p)!=EOF)
 {
 if(p==0)
 {
  if(n==1)
  {
   if(s==0)printf("0\n");
   else printf("No solution\n");
  }
  else
  {
   for(i=1;i<n;i++)
   printf("0 ");
   printf("%I64d\n",s);
  }
 }
 else //p!=0
 {total=0;
  if(p<0)m=-p;
  else m=p;
  k=isprime(m);
  if(k==1)
  {
   xp[total]=-1;xp[++total]=1;
   xp[++total]=m;xp[++total]=-m;
   total++;
  }//if
  else
  {total1=0;
  p0[total1]=1;total1++;
  if(m!=1)
  fenjie(m);
  //printf("%d\n",total1);
  for(i=0;i<total1;i++)
  {
   xp[total]=p0[i];xp[++total]=-p0[i];total++;
  }
  
  }//else
 // printf("%d\n",x[2]);
  /*for(i=0;i<total;i++)
  printf("%d ",xp[i]);
  printf("\n");
  */
  sum[0]=0;fact[0]=1;count=0;
  recur(1);
  if(count==0)printf("No solution\n");
  }//else
  
 }//while
}//main

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator