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. Entity Framework part2

    EF原理以XML方式打开edmx文件,这个XML的文件主要包含两大部分:Runtime是类模型部分,Designer是VS中的图形界面重点讨论的是Runtime部分,又分为三大部分:SSDL数据模型部 ...

  2. JAVA 基本概念和编码规范

    概括性描述:一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作. 基本概念: 下面简要介绍下类.对象.方法和属性的概念. 对象:对象是类的一个实例,有状态和行为.例如, ...

  3. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.6.集群管理命令

    3.6. 集群管理命令 3.6.1. RAC的启动与关闭 oracle rac默认会开机自启动,如需维护时可使用以下命令: 关闭: crsctl stop cluster 停止本节点集群服务 crsc ...

  4. pwn2own

    Pwn2Own是全球最著名的黑客大赛之一,由美国五角大楼入侵防护系统供应商TippingPoint的DVLabs赞助,今年已经是第六届. 1比赛规则 参赛黑客们的目标是4大主流网页浏览器——IE.Fi ...

  5. artDialog弹出框使用

    配置参数 名称 类型 默认值 描述 内容 title String '消息' 标题内容 content String 'loading..' 消息内容.1.如果传入的是HTMLElement类型,如果 ...

  6. infer 编译代码审查命令记录

    infer -- xcodebuild -target <target name> -configuration <build configuration> -sdk ipho ...

  7. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  8. 小米6安装google play

    http://bbs.xiaomi.cn/t-13579116 http://m.mk52.cn/jiaocheng/3288.html 步骤: 1.下载需要的文件并解压 (http://techta ...

  9. ActiveMQ简述

    概述 ActiveMQ是Apache所提供的一个开源的消息系统,全然採用Java来实现.因此.它能非常好地支持J2EE提出的JMS(Java Message Service,即Java消息服务)规范. ...

  10. js实现select跳转

    js简单实现select跳转功能:代码例如以下 <!DOCTYPE html> <html> <head> <title></title> ...