欧拉函数~求互质对数
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
1到n中,任意选择两个数,使其互质,问总共有多少种选择方法,注意(1,2)和(2,1)是同一种方案
输入有多组数据,第一行输入T(T<=100000)
接下来每一行输入一个n,(1<=n<=1000)
每一行输出一个方案数
12
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;
}
练习: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;
}
相关文章推荐
- SpringMVC+Mybatis框架整合源码
- 展示C代码覆盖率的gcovr工具简介及相关命令使用示例
- scrollview/tableview 无法响应touch事件
- PHP(一)简介
- 修改host文件
- tuxedo tpcall 返回6 的问题
- 易学02
- C#获取Honeywell voyager 1400g扫码后的数据
- Jetty and Tomcat handle Json bug eval() method
- 元素绘制顺序
- Codeforces Round #313 (Div. 2) 560D Equivalent Strings(dos)
- 工作遇到的问题备忘
- Uiautomator——API详解
- 修改在VS2010环境下建立的C++项目名称
- django-uuslug: 方便的将中文转化成拼音 slug 的工具
- 块级元素和内联元素的区别(HTML)
- 精心收集的Hadoop学习资料(持续更新)
- Android实战 - 音心播放器 (MusicListActivity - 分类信息界面实现)
- hdu1398 Square Coins (完全背包)
- Linux Power Managment详解