您的位置:首页 > 其它

欧拉函数~求互质对数

2015-12-18 17:09 246 查看
运用欧拉函数可以求出与n互质的个数,或n以内与x互质的个数 。http://www.cnblogs.com/machen/articles/4829187.html

练习:http://gdutcode.sinaapp.com/problem.php?cid=1027&pid=3


Problem D: 求互质对数


Description

 1到n中,任意选择两个数,使其互质,问总共有多少种选择方法,注意(1,2)和(2,1)是同一种方案


Input

输入有多组数据,第一行输入T(T<=100000)

接下来每一行输入一个n,(1<=n<=1000)


Output

 每一行输出一个方案数


Sample Input

12


Sample Output

1

代码也可以直接这样写:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

typedef long long LL;

int a[1000+50];

int gcd(int a,int b)
{
return b == 0 ? a : gcd(b,a % b);
}

void fun()
{
a[0] = 0;
for(int i = 1; i <= 1000; i ++)
{
int cnt = 0;
for(int j = 1; j < i; j ++)
if(gcd(i,j) == 1)cnt ++;
a[i] = a[i - 1] + cnt;
}
}
int main()
{
// freopen("in.txt","r",stdin);
int t;
fun();
scanf("%d",&t);
while(t --)
{
int n;
scanf("%d",&n);
printf("%d\n",a
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: