题目大意:N以内有多少对互质的数。
以下是我的代码:
#include <iostream>
using namespace std;
const int kMaxn = 50000;
int phi[kMaxn+7], sum[kMaxn+7];
void Init ()
{
phi[1] = 1;
for ( int i = 2; i <= kMaxn; i++ )
phi[i] = 0;
for ( int i = 2; i <= kMaxn; i++ )
if ( !phi[i] )
for ( int j = i; j <= kMaxn; j+=i )
{
if ( !phi[j] )
phi[j] = j;
phi[j] = phi[j] / i * ( i - 1 );
}
sum[1] = 1;
for ( int i = 2; i <= kMaxn; i++ )
sum[i] = sum[i-1] + ( phi[i] << 1 );
}
int main ()
{
Init ();
int n;
while ( cin >> n && n )
cout << sum[n] << endl;
return 0;
}
posted on 2011-09-20 19:49
lee1r 阅读(721)
评论(0) 编辑 收藏 引用 所属分类:
题目分类:数学/数论