hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数
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; 容斥或者欧拉函数的更多相关文章
- 【hdu-2588】GCD(容斥定理+欧拉函数+GCD()原理)
GCD Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- hdoj 1787 GCD Again【欧拉函数】
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
- 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 ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- HDU2588:GCD(欧拉函数的应用)
题目链接:传送门 题目需求:Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.(2& ...
- (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 ...
- hdu2588 gcd 欧拉函数
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
随机推荐
- Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis
https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...
- oracle--v$lock type字段详解
Name Description AD ASM Disk AU Lock AF Advisor Framework AG Analytic Workspace Generation AK GES De ...
- ActiveMQ实战-集群
原文:http://blog.csdn.net/lifetragedy/article/details/51869032 ActiveMQ的集群 内嵌代理所引发的问题: 消息过载 管理混乱 如何解决这 ...
- wpf datagrid performance
http://stackoverflow.com/questions/1704512/wpf-toolkit-datagrid-scrolling-performance-problems-why h ...
- redis深入研究
Redis 设计思路学习与总结 https://cloud.tencent.com/developer/article/1004464 Redis 设计思路学习与总结 下半年利用空余时间研究和分析了部 ...
- C#断点续传
我们经常使用下载工具,如bit精灵.迅雷.FlashGet,这些软件都支持断点续传. 断点续传即下载任务暂停后可以继续,而无需重新下载,即下载时需要通知服务器的起始位置.如果允许多线程进行分片下载,必 ...
- windowsclient开发--使用、屏蔽一些快捷键
每一个windowsclient都有自己的一些快捷键,有的是windows系统提供的. 今天就要与大家分享一下.在windowsclient开发过程中对按键的处理. ESC按键 Duilib这个库中, ...
- centos7安装ifconfig命令
ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息,可是有些时候最小化安装CentOS等Linux发行版的时候会默认不安装ifconfig等命令,这时候你进入终端,运行ifc ...
- wordpress 插件 之 微信自动回复机器人
微信目前越来越火,wordpress 也拥有众多用户 那我们来写个插件,把两者连起来吧! 目前已经测试完成,下面把相关的信息分享一下. 查看演示 请加我微信公众号 创新实验室,或直接扫描最底下的二维码 ...
- B7:访问者模式 Visitor
表示一个作用于某对象结构中各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 适用场景:1.适用于元素类数据结构相对稳定(类的方法固定,但属性可以变化,如果方法变化很大,就无 ...