题目:http://poj.org/problem?id=1528
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11909 | Accepted: 5595 |
Description
Input
Output
Sample Input
15 28 6 56 60000 22 496 0
Sample Output
PERFECTION OUTPUT 15 DEFICIENT 28 PERFECT 6 PERFECT 56 ABUNDANT 60000 ABUNDANT 22 DEFICIENT 496 PERFECT END OF OUTPUT分析:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int fac[300],p[300],top; void resolve(int x){ top=0; memset(p,0,sizeof(p)); for(int i=2;i*i<=x;i++){ if(x%i==0){ fac[top]=i; while(x%i==0){ x/=i; p[top]++; } top++; } } if(x>1){ fac[top]=x; p[top++]++; } } int power(int a,int n){ int ans=1,temp=a; while(n){ if(n&1) ans=ans*temp; temp=temp*temp; n>>=1; } return ans; } int cal(int a,int n){ if(n==2) return (1+a+a*a); if(n==1) return (1+a); if(n&1) return (1+power(a,n/2+1))*cal(a,n/2); else return (1+power(a,(n/2+1)))*cal(a,(n/2-1))+power(a,(n/2)); } int main() { //freopen("cin.txt","r",stdin); int a; printf("PERFECTION OUTPUT\n"); while(cin>>a&&a){ resolve(a); int res=1; for(int i=0;i<top;i++){ res=res*cal(fac[i],p[i]); } printf("%5d ",a); res=res-a; if(res<a) puts("DEFICIENT"); else if(res==a) puts("PERFECT"); else puts("ABUNDANT"); } printf("END OF OUTPUT\n"); return 0; }java:
import java.util.*; import java.lang.String; public class Main { static int[] fac=new int [300],p=new int [300]; static int top; static void resolve(int x){ top=0; Arrays.fill(p,0); //Arrays belong to util for(int i=2;i*i<=x;i++){ if(x%i==0){ fac[top]=i; while(x%i==0){ x/=i; p[top]++; } top++; } } if(x>1){ fac[top]=x; p[top++]++; } } static int power(int a,int n){ int ans=1,temp=a; while(n>0){ if(n%2==1) ans=ans*temp; temp=temp*temp; n>>=1; } return ans; } static int cal(int a,int n){ if(n==2) return (1+a+a*a); if(n==1) return (1+a); if(n%2==1) return (1+power(a,n/2+1))*cal(a,n/2); else return (1+power(a,(n/2+1)))*cal(a,(n/2-1))+power(a,(n/2)); } public static void main(String[] args) { int a; Scanner sc=new Scanner(System.in); System.out.println("PERFECTION OUTPUT"); while(sc.hasNextInt()){ a=sc.nextInt(); if(a==0) break; resolve(a); int res=1; for(int i=0;i<top;i++){ res=res*cal(fac[i],p[i]); } String str; res=res-a; if(res<a) str="DEFICIENT"; else if(res==a) str="PERFECT"; else str="ABUNDANT"; System.out.printf("%5d %s\n",a,str); } System.out.printf("END OF OUTPUT\n"); } } /*class perfect is public, should be declared in a file named perfect.java public class perfect { ^ 把perfect 改成Main即可。(public修饰的类名必须和文件名一样) */