POJ 2407 Relatives
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8923 | Accepted: 4184 | 
Description
Input
Output
Sample Input
7 12 0
Sample Output
6 4
Source
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <string.h>
#define N 100000
using namespace std;
bool hash[N];
bool is_p(int &n)
{
    if(n%2==0) return false;
    int i,m=sqrt(double(n));
    for(i=3;i<=m;i+=2)
     if(n%i==0)
       return false;
    return true;
}
int rc[10000],l;
void set()
{
     int i,j;
    for(i=4;i<N;i+=2)
      hash[i]=1;
    for(i=3;i<N;i+=2)
       if(is_p(i))
         for(j=i+i;j<N;j+=i)
           hash[j]=1;
    j=0;
    for(i=2;i<N;i++)
     if(!hash[i])
      rc[j++]=i;
    l=j;
}
int main()
{
    set();
    int n,i;
    double s;
    bool b;
    while(scanf("%d",&n),n)
    {
        s=n;i=0;
        while(n!=1)
        {    b=0;
            if(is_p(n)){s=s*(1.0-1.0/n);break;}
            while(n%rc[i]==0)
            {
                b=1;
                n=n/rc[i];
            }
            if(b) {s*=(1.0-1.0/rc[i]);}
            i++;
        }
       n=s+0.5;
       printf("%d\n",n);
    }
    return 0;
}
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号