B - Relatives
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.
Input
There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.
Output
For each test case there should be single line of output answering the question posed above.
Sample Input
7 12 0
Sample Output
6 4
!!!!思路:寻找从1到n-1里与n互质的数的个数!!!!(1与任何数都互质)
利用欧拉函数进行求解。
本人不会写 百度到的模板- - 汗(⊙﹏⊙)b
#include<stdio.h>
#include<math.h>
int main()
{
int n;
while(scanf("%d",&n)&&n!=0)
{
if(n==1)
printf("0\n");
else
{
int i,j,m;
m=sqrt(n+0.5);
int ans=n;
for(i=2;i<=m;i++)
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0)
n=n/i;
}
if(n>1)
ans=ans/n*(n-1);
printf("%d\n",ans);
}
}
return 0;
}
我在努力,虽然依旧很菜。
浙公网安备 33010602011771号