Send a Table UVA - 10820
题意:
给你n*n个二元组,要求你统计要预先算出多少个(x,y)。(其中(kx,ky)可以通过(x,y)推出来)。
思路:
本质
:
输入n,有多少个二元组(x,y)满足:1<=x,y<=y,且x和y互素。不难发现除了(1,1)之外,其他满足条件的二元组(x,y)中的x和y都不相等。假设满足x<y的二元组有f(n)个,那么ans = 2*f(n)+1。
根据欧拉函数的定义
f ( n ) = p h i ( 2 ) + p h i ( 3 ) + . . . + p h i ( n ) f(n) = phi(2)+phi(3)+...+phi(n) f(n)=phi(2)+phi(3)+...+phi(n)
AC
#include <iostream>
#include <algorithm>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
using namespace std;
const int maxn = 5e4+10;
typedef long long ll;
int phi[maxn];
ll pre[maxn];
void table(){
fori(i,2,maxn)phi[i] = 0;
phi[1] = 1;
fori(i,2,maxn){
if(!phi[i]){
for(int j = i; j<maxn; j += i){
if(!phi[j])phi[j] = j;
phi[j] = phi[j] / i * (i-1);
}
}
}
fori(i,2,maxn)pre[i] = pre[i-1] + phi[i];
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
table();
while(cin>>n,n){
cout<<(pre[n]*2+1)<<endl;
}
return 0;
}