Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
0ms水过贴源代码于此~不过吃了很大亏啊!切记能不用浮点型就不用!#include<stdio.h> #define N 31700 int t,a[N]={0},b[N]={0}; void init() { int i,j; for (i=2;i*i<=N;i++) if (!a[i]) for (j=i*i;j<N;j+=i) a[j]=1; t=1; for (i=2;i<=N;i++) if (!a[i]) b[t++]=i; } int prime(int x) { for (int i=1;;i++) if (b[i]*b[i]>x) return 1; else if (x%b[i]==0) return 0; } int main() { int n,x,i,s; init(); while (scanf("%d",&n)&&n) if (n==1) printf("0\n"); else { x=n; i=1; s=n; for (i=1;i<=t;i++) if (prime(x)) { s=s/x*(x-1); break; } else if (x%b[i]==0) { s=s/b[i]*(b[i]-1); while (x%b[i]==0) x/=b[i]; if (x==1) break; } printf("%d\n",s); } return 0; } 注意:原来s=s/b[i]*(b[i]-1)(s为int)不是这样写的,是s=s*b[i-1]/b[i](s为double)可是这样写就是wa啊! Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator