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. path node

    process.cwd() 当前Node.js进程执行时的工作目录 __dirname 当前模块的目录名 const path = require('path'); console.log(__dir ...

  2. Django-6 Django ORM层

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...

  3. HNOI2019游记

    \(day~?\) 我们的老师告诉我说,你这次省选目标分:\(70\),拿不到,家法伺候.但其实,我的目标是不爆零!!! \(day~-1\) 这天晚上,我们的指导老师给我们试了一下ZJOI2019, ...

  4. Activiti6-IdentityService(学习笔记)

    IdentityService并不依赖我们的流程部署文件,所以 直接放使用方法的测试代码了: public class IdentityServiceTest { private static fin ...

  5. vue cli使用融云实现聊天

    公司有个项目要实现一个聊天功能,需求如下图,略显随意 公司最终选择融云这个吊炸天的即时通信,文档详细的一匹,刚开始看文档感觉很详细实现起来也不麻烦,有很多开源的demo可以在线演示和下载 不过我们的项 ...

  6. sprigboot recontroller 是responsebody与controller结合 这样 就使每个方法默认返回json

    sprigboot   recontroller 是responsebody与controller结合 这样 就使每个方法默认返回json

  7. found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, or `npm audit` for details

    npm 安装包之后,如果出现类似下面的信息 found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, ...

  8. WC2019冬眠记

    Day0 报道日就当做Day0吧. 上午起床比较晚,起来就开始整理东西准备搬到广二的高中部去,搬了两趟,因为没吃早饭,头就很晕,吓得我赶快把之前发的士力架给吃了. 上午李姐姐和我们聊了聊\(THUWC ...

  9. 20165223《网络对抗技术》Exp1 PC平台逆向破解

    目录--PC平台逆向破解 1 逆向及BOF基础实践说明 1.1 实践内容 1.2 实践要求 1.3 基础知识 2 实验步骤 2.1 直接修改程序机器指令,改变程序执行流程 2.2 通过构造输入参数,造 ...

  10. C++ 黑白棋AI minimax+alphabeta剪枝

    没事写着玩玩,通过debian上的黑白棋测试,搜了10层,打hard应该问题不大 #include <cstdio> #include <cstring> using name ...