GCD
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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,(,)=,(,)=.
(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 <=X<=N and (X,N)>=M. Input
The first line of input is an integer T(T<=) representing the number of test cases. The following T lines each contains two numbers N and M (<=N<=, <=M<=N), representing a test case. Output
For each test case,output the answer on a single line. Sample Input Sample Output 欧拉函数做法:
/**
题目:GCD
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2588
题意:给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m;
思路:
x -> [1,n] d = gcd(x,n) >= m d肯定为n的约数。 对一个确定的d = gcd(x,n); 那么:gcd(x/d,n/d) = 1; 满足上面式子的x为:f(n/d); f(y)表示y的欧拉函数。 sigma(f(n/d)) (d为n的约数且d>=m); f(y) = y*(p1-1)/p1*(p2-1)/p2...*(pe-1)/pe; */
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll Euler(ll x)
{
ll n = x;
for(int i = ; i*i<=x; i++){
if(x%i==){
n = n/i*(i-);
while(x%i==)x/=i;
}
}
if(x>){
n = n/x*(x-);
}
return n;
}
vector<int> v;
int main()
{
int T;
int n, m;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
v.clear();
for(int i = ; i*i<=n; i++){
if(n%i==){
if(i*i==n){
if(i>=m) v.push_back(i);
}
else{
if(i>=m) v.push_back(i);
if(n/i>=m) v.push_back(n/i);
}
}
}
int len = v.size();
int cnt = ;
for(int i = ; i < len; i++){
cnt += Euler(n/v[i]);
}
printf("%d\n",cnt);
}
return ;
} 容斥做法:
/**
题目:GCD
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2588
题意:给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m;
思路: 显然d = gcd(x,n)中的d一定是n的约数。 显然d = gcd(d,n); 先获得所有>=m的d; 那么d的倍数为x=k*d,如果小于等于n,则一定也满足gcd(x,n)>=m; k = n/d; 如果对每个d这样计算,会有重复的计算。 当d = 2, 3时候,x=6会多计算一次。 所以要对所有的d进行容斥处理。 问题转化为:n的约数为d,求解d>=m的所有的d在n范围内至少有一个是d的倍数的数有多少个。 */
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
vector<int> v;
int a[], z; ///注意a数组要开大些,约数个数还是不是100就够的。
ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
}
ll rc(ll n)
{
ll sum = ;
ll mult;
int ones;
int len = v.size();
int m = (<<len);
//奇加偶减
for(int i = ; i < m; i++){
ones = ;
mult = ;
for(int j = ; j<len; j++){
if(i&(<<j)){
ones++;
mult = mult/gcd(mult,v[j])*v[j];
if(mult>n) break;
}
}
if(ones%==){
sum -= n/mult;
}else
{
sum += n/mult;
}
}
return sum;
}
int main()
{
int T;
int n, m;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&m);
v.clear();
z = ;
for(int i = ; i*i<=n; i++){
if(n%i==){
if(i*i==n){
if(i>=m) a[z++] = i;
}
else{
if(i>=m) a[z++] = i;
if(n/i>=m) a[z++] = n/i;
}
}
}
///出去包含的,比如2,4那么4要去掉。以为4的倍数一定是2的倍数。
sort(a,a+z);
for(int i = ; i < z; i++){
int sign = ;
for(int j = ; j < i; j++){
if(a[i]%a[j]==){
sign = ; break;
}
}
if(sign==){
v.push_back(a[i]);
}
}
printf("%lld\n",rc(n));
}
return ;
}

hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数的更多相关文章

  1. 【hdu-2588】GCD(容斥定理+欧拉函数+GCD()原理)

    GCD Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  2. hdoj 1787 GCD Again【欧拉函数】

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. BZOJ2818: Gcd 欧拉函数求前缀和

    给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...

  4. Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。

    /** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...

  5. hdu2588 GCD (欧拉函数)

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

  6. HDU2588:GCD(欧拉函数的应用)

    题目链接:传送门 题目需求:Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.(2& ...

  7. (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

    题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. hdu2588 gcd 欧拉函数

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

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

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

随机推荐

  1. Windows 系统下设置Nodejs NPM全局路径和环境变量配置

    在nodejs的安装目录中找到node_modules\npm\.npmrc文件 修改如下即可: prefix = D:\tool\nodejs\node_globalcache = D:\tool\ ...

  2. iOS键盘监听事件

    1.注册键盘通知事件 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 键盘将出现事件监听 [center ...

  3. B/S架构 Web打印程序(Argox)

    B/S架构 Web打印程序 对于B/S架构下的管理软件系统,在客户端浏览器页面中如何透过本地打印机或网络打印机打印条码标签,Argox提供最常用的2种类型打印机集成方案. ●  PPLA 打印机IE游 ...

  4. WPF 中依赖属性的继承(Inherits)

    WPF中依赖属性的值是是可以设置为可继承(Inherits)的,这种模式下,父节点的依赖属性会将其值传递给子节点.例如,数据绑定中经常使用的DataContextProperty: var host ...

  5. PHP将字符串首字母大小写转换

    每个单词的首字母转换为大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo); // Hello World! $bar = ...

  6. Shiro+SpringMVC 实现更安全的登录(加密匹配&登录失败超次数锁定帐号)

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/48518003 前言 初学shiro,shiro提供了一系列安全相关的解决方案,根据官方的介绍 ...

  7. 安装完office2016 64位后,在安装visio时,报错,无法安装,

    安装环境要求: 系统要求:win8,win10等: office要求:sw(批量版)不能和cn(零售版).365版混装.-------重点注意事项 一定要注意批量版和零售版的区别,各版本之间绝对不允许 ...

  8. 在eclipse上配置tomcat

    Eclipse中Tomcat的配置及简单例子 Eclipse中Tomcat的配置是很简单的一个工作,作为一名刚刚起步的编程菜鸟,我将这个配置的过程和简单的例子写下来记录,也希望能给像我怎样的新手一些帮 ...

  9. Java solr 索引数据增删改查

    具体代码如下: import java.io.IOException; import java.util.*; import org.apache.solr.client.solrj.SolrClie ...

  10. [Python爬虫] 之二十:Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据

    一.介绍 本例子用Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据()的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯 ...