GCD

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 76 Accepted Submission(s): 50
 
Problem Description
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, how many integer X satisfies 1<=X<=N and (X,N)>=M.
 
Input
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 (2<=N<=1000000000, 1<=M<=N), representing a test case.
 
Output
For each test case,output the answer on a single line.
 
Sample Input
3
1 1
10 2
10000 72
 
Sample Output
1
6
260
 
 
Source
ECJTU 2009 Spring Contest
 
Recommend
lcy
/*
题意:给出N,M让你求出 X的个数 ,X满足GCD(X,N)>=M; 初步思路:首先N的比M大的因子肯定是,是这个因子的倍数的也是。除此之外就没了,因为其他的数GCD(other,N)=1,如果M等于1的话,直接输出N就行了
现在的问题就是怎么找因子的倍数,因为会有重复的,res=N/x(x是N的因子);对于因子x有res个可满足的结果,但是在计算过程中会有重复的存在,
这样,令pi<=res && GCD(pi,res)==1,这样保证了 pi*x不会重复,就转化成了,求N/x的欧拉函数 */
#include<bits/stdc++.h>
using namespace std;
/**************************欧拉函数模板*****************************/
//直接求解欧拉函数
int euler(int n){ //返回euler(n)
int res=n,a=n;
for(int i=;i*i<=a;i++){
if(a%i==){
res=res/i*(i-);//先进行除法是为了防止中间数据的溢出
while(a%i==) a/=i;
}
}
if(a>) res=res/a*(a-);
return res;
}
/**************************欧拉函数模板*****************************/
int solve(int n,int m){
if(m==) return n;
int cur=;
for(int i=;i*i<=n;i++){
if(n%i==){//i是n的因子
if(i>=m){
cur+=euler(n/i);
}
if(i*i!=n){//对面的因子
if(n/i>=m){
cur+=euler(n/(n/i));
}
}
}
}
return cur+;
}
int t;
int n,m; int main(){
//freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
printf("%d\n",solve(n,m));
}
return ;
}

最新文章

  1. 基于UDP的网络编程
  2. QT操作EXCEL
  3. android HAL 教程(含实例)
  4. 所谓完整的linux系统包括哪些部分呢?【转】
  5. DRUPAL点滴
  6. Window8 进不了PE如何设置BIOS
  7. MyBatis3.1 学习教程
  8. 如何在myeclipse8.5中使用maven
  9. Javascript quiz
  10. nodejs学习笔记之网络编程
  11. 【Java之】多线程学习笔记
  12. easyui datagrid 单元格编辑 自动聚焦 、全选
  13. 利用svn自动同步更新到网站服务器 -- 网摘
  14. 开发中常用的 $.extend 总结
  15. vue实现登录后跳转到之前的页面
  16. IOS中用到的缓存
  17. Neovim中NERDTree等多处cursorline不高亮
  18. 05 树莓派安装飞鸽传书 Iptux
  19. Go语言总结
  20. postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)

热门文章

  1. Https系列之二:https的SSL证书在服务器端的部署,基于tomcat,spring boot
  2. Java笔记—— 类与对象的几个例子
  3. 如何关闭eclipse对js xml的验证
  4. Perfect Pth Powers poj1730
  5. SQL语句表名或者字段名和保留字冲突解决方法
  6. Jmeter的安装和启动时出现unable to access jarfile apachejmeter.jar error value=1错误处理
  7. Android 实现UI设计
  8. ch5-Class 与 Style 绑定(v-bind:class v-bind:style)
  9. HDU2036 改革春风吹满地
  10. nodejs+express-实现文件上传下载管理的网站