GCD  nyoj 1007 (欧拉函数+欧几里得)

GCD

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M,please answer sum of  X satisfies 1<=X<=N and (X,N)>=M.
 
输入
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (1<=N<=10^9, 1<=M<=10^9), representing a test case.
输出
Output the answer mod 1000000007
样例输入
3
1 1
10 2
10000 72
样例输出
1
35
1305000 题意:1<= x <= n ,求gcd(x,n) >= m 说有满足条件的x的和 (最后要模Mod=1000000007)
分析:
如果是求满足条件的x的个数:
因为x要满足1<=x<=n 且gcd(x,n)>=m,所以x为n的因子,即gcd(x,n)=x>=m,设y=n/x,
则y的欧拉函数为小于y且与y互质的数的个数。假设与y互质的数为p1,p2,p3……,那么gcd(x*pi,n)=x>=m.
即要找出所有符合要求的y的欧拉函数值的和即可。 因为1<= x <= n 若gcd(x,n) = x >= m 推出x是n的因子 y = n/x 与y互质且小于y的数pi 总共有eular(y)个
gcd(x*pi,n) = x >= m 所以此时满足条件的x的个数有eular(y)个
又因为 gcd(a,n) = gcd(n-a,n) 【定理记住】 所以此条件下x的和sum = n*eular(y)/2
最后再依此求出n的所有因子 计算sum相加即可 计算x的总和:附上代码
/*
author:谦智
HDU 2588-GCD(欧拉函数) 数论
*/
#include<iostream>
using namespace std;
const int Mod = ;
#define LL long long
LL eular(LL n) ;
LL eularSum(LL k) {
if (k == ) return ;//特殊值
return k*eular(k)/;
}
int main() {
int t;
cin >> t;
while (t--) {
LL n, m;
cin >> n >> m;
LL sum = ;
if (n == && m == ) {//特殊情况
cout << << endl;
continue;
}
for (LL i = ; i*i <= n; i++) {
if (n%i == ) {
if (i >= m) {
sum = (sum + n*eular(n/i)/)%Mod; //==》 sum = (sum + i*(n/i)*eular(n/i)/2)%Mod
// sum = (sum + i*eularSum(n/i))%Mod;//总共有 eular(n/i)个x使得gcd(x,n) >= m
}
if (n/i >= m && i*i != n) {
      //只需要在这里加一个i== 1的情况上面不要加 不然会重复
if (i == ) sum = (sum + n)%Mod;//当eular(i) < 2时只需要加上此时唯一满足条件的x即可 其他的eular(i)一定都是偶数因为gcd(a,n)= gcd(n-a,n)
else sum = (sum + n*eular(i)/)%Mod;
// sum = (sum + n/i*eularSum(i))%Mod;
}
}
}
cout << sum << endl;
}
}
LL eular(LL n) {
LL ans = n;
for (LL i = ; i*i <= n; i++) {
if (n%i == ) {
ans = ans/i*(i-);
while (n%i == ) {
n /= i;
}
}
}
if (n != ) ans = ans/n*(n-);
return ans;
}

 

计算x的个数:附上代码

//计算 x小于n 且gcd(x,n) >= m 的x的个数
#include<iostream>
using namespace std;
int eular(int n) ;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
int sum = ;
for (int i = ; i*i <= n; i++) {
if (n%i == ) {
if (i >= m) {
sum += eular(n/i);
} else if (n/i >= m && i*i != n) {
sum += eular(i);
}
}
}
cout << sum << endl;
}
}
int eular(int n) {
int ans = n;
for (int i = ; i*i <= n; i++) {
if (n%i == ) {
ans = ans/i*(i-);
}
while (n%i == ) {
n /= i;
}
}
if (n != ) ans = ans/n*(n-);
return ans;
}
 

 

GCD nyoj 1007 (欧拉函数+欧几里得)的更多相关文章

  1. 【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛

    题目描述 给出 $n$ 和 $p$ ,求 $(\sum\limits_{i=1}^n\sum\limits_{j=1}^nij\gcd(i,j))\mod p$ . $n\le 10^{10}$ . ...

  2. 【poj2478-Farey Sequence】递推求欧拉函数-欧拉函数的几个性质和推论

    http://poj.org/problem?id=2478 题意:给定一个数x,求<=x的数的欧拉函数值的和.(x<=10^6) 题解:数据范围比较大,像poj1248一样的做法是不可行 ...

  3. hdoj 1286 找新朋友【欧拉函数】

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. HDU 3501【欧拉函数拓展】

    欧拉函数 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) . 通式:φ(x)=x*(1-1/p1)(1-1/p2)(1-1/p3)*(1-1/p4)-..(1- ...

  5. 线段树+欧拉函数——cf1114F

    调了半天,写线段树老是写炸 /* 两个操作 1.区间乘法 2.区间乘积询问欧拉函数 欧拉函数计算公式 phi(mul(ai))=mul(ai) * (p1-1)/p1 * (p2-1)/p2 * .. ...

  6. nyoj 1007 GCD(数学题 欧拉函数的应用)

    GCD 描述 The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b) ...

  7. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  8. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  9. poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

    这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...

随机推荐

  1. Struts2+Spring+Hibernate3整合

    这几天正在复习三大框架的知识,特意把写出来,如有错误,希望大家多指教! 代码地址:https://git.coding.net/puchenglin/SSHDemo.git 1. 引入jar包 Str ...

  2. 结巴分词出现AttributeError: 'float' object has no attribute 'decode'错误

    将data转变为str格式 inputfile = 'comment2.csv'outputfile = 'comment2_cut.txt'datas = pd.read_csv(inputfile ...

  3. content-type 组件

    content-type初识 什么是content-type ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中 ...

  4. java 键盘录入(Scanner)

    键盘录入(Scanner)• 键盘录入数据概述– 我们目前在写程序的时候, 数据值都是固定的, 但是实际开发中, 数据值肯定是变化的, 所以, 把数据改进为键盘录入, 提高程序的灵活性.• 如何实现键 ...

  5. 解决php -v查看到版本与phpinfo()版本不一致问题

    安装p7后发现phpinfo的版本是7.2.12,而php -v查看的却是5.4.16 应该是php.ini的配置文件有问题. 查看文件,有两个 查看cli执行的文件是哪一个? 再查看phpinfo用 ...

  6. 1.9 分布式协调服务-Zookeeper(二)

    zoo.cfg配置文件分析 tickTime=2000  zookeeper中最小的时间单位长度 (ms) initLimit=10  follower节点启动后与leader节点完成数据同步的时间 ...

  7. Linux(Centos)设置ip

    一.Linux(Centos7)安装完毕后,重启电脑后,输入用户名(root),密码(**********). 在命令行中输入nmtui命令.选择Edit a connection,选择对应的链接端口 ...

  8. mongodb副本集原理及部署记录

    工作原理 1.副本集之间的复制是通过oplog日志现实的.备份节点通过查询这个集合就可以知道需要进行复制的操作 2.oplog是节点中local库中的一个固定的集合,在默认情况下oplog初始化大小为 ...

  9. Consul1-window安装consul

    转自  https://blog.csdn.net/j903829182/article/details/80960802 consul下载地址: https://www.consul.io/down ...

  10. BeanUtils 日期转换(本地格式yyyy-MM-dd)转换成date

    1.BeanUtils工具的使用 1)beanUtils 可以便于对javaBean的属性进行赋值. 2)beanUtils 可以便于对javaBean的对象进行赋值. 3)beanUtils可以将一 ...