hdu1286(找新朋友)&&POJ2407Relatives(欧拉函数模版题)

 http://acm.hdu.edu.cn/showproblem.php?pid=1286

没什么好说的,模板题,主要是弄懂欧拉函数的思想。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int T,temp,ans,n;
    cin>>T;
    while(T--)
    {
        cin>>n;
        temp=n;
        ans=n;
        for(int i=2; i*i<=n; i++)
        {
            if(n%i==0)
            {
                ans=ans/i*(i-1);
                n/=i;
                while(n%i==0)
                {
                    n/=i;
                }
            }
        }
        if(n!=1)
        {
            ans=ans/n*(n-1);
        }
        printf("%d
",temp==1?0:ans);
    }
    return 0;
}

 POJ2407:http://poj.org/problem?id=2407

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define N 65540
using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        int ans=n;
        int t=ceil(sqrt(n*1.0));
        for(int i=2;i<=t;i++)
        {
            if(n%i==0)
            {
                ans=ans-ans/i;
                do
                {
                    n/=i;
                }
                while(n%i==0);
            }
        }
        if(n!=1)
        ans=ans-ans/n;
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangmingcheng/p/4115987.html