POJ 2407 Relatives UVA 10299 Relatives(欧拉函数)

【题目链接】:click here~~

【题目大意】:欧拉函数:求少于或等于n的数中与n互素的数的个数;n <= 1,000,000,000。

【思路】:裸欧拉函数,注意特判n==1的情况,n==1的情况下,应该输出0,poj依然判断1也可以过,但是老牌ojUVA必须是0才过,注意一下。

代码:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;int eular(int n){int res=n;for(int i=2; i*i<=n; ++i){if(n%i==0){n/=i;res=res-res/i;while(n%i==0){n/=i;}}}if(n!=1) res=res-res/n;return res;}int main(){int n;while(cin>>n&&n!=0){if(n==1) puts("0");else printf("%d\n",eular(n));} return 0;}

版权声明:本文为博主原创文章,,未经博主允许不得转载。

你曾经说,最大的愿望,

POJ 2407 Relatives UVA 10299 Relatives(欧拉函数)

相关文章:

你感兴趣的文章:

标签云: