http://poj.org/problem?id=2407
求n以内互素的无序数对数(欧拉函数打表)
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
Source Code:
#include<iostream>
#define N 1000010
#define ll long long
using namespace std;
ll phi[N],ans[N];
ll n;
void Euler()//打表N以内数的欧拉函数
{
for(ll i=1;i<=N;i++)
phi[i]=i;
for(ll i=2;i<=N;i+=2)
phi[i]/=2;
for(ll i=3;i<=N;i+=2)
{
if(phi[i]==i)
{
for(ll j=i;j<=N;j+=i)
phi[j]=phi[j]/i*(i-1);
}
}
}
int main()
{
Euler();
while(cin>>n&&n)
{
for(ll i=2;i<=n;i++)
ans[i]=ans[i-1]+phi[i];
cout<<ans[n]<<endl;
}
return 0;
}