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 ...
随机推荐
- SpingMVC实现操作的第一方式
(一) 使用SpringMVC框架的步骤 (1):在web.xml中配置前端控制器 (2):在Spring-servlet.xml中配置 配置处理器映射器HandlerMapping(处理器Handl ...
- javolution-core-java-6.1.0.jar 的使用
官方网址:http://javolution.org/apidocs/javolution/io/Struct.html 第一步:导包 第二步:创建继承的结构体 结构体定义如下所示: public c ...
- 如何让Adobe reader 记住上次pdf文档打开位置?
菜单栏: Edit --> Preferences --> Documents --> 勾选 “Restore last view settings where reopening ...
- MythXinWCF通用宿主绿色版发布(一键启动,方便快捷)
这是我开发的一款免费的,绿色的WCF宿主工具,一键启动,方便快捷,省去了安装和配置IIS的麻烦,也不需要配置文件. 该工具运行环境为.Net Framework 4.5.1(这个是必装的,win10自 ...
- Matlab自带的曲线拟合程序
这个函数的功能是能自动搜索参数的取值,从而使得方程的误差最小. 效果如下 代码如下 %% Optimal Fit of a Non-linear Function % This is a demons ...
- Android应用内 代码截屏(获取View快照)和 禁止截屏
1. 应用内的代码截屏(获取View的快照) Android的View类中提供了获取控件绘制缓存的方法,这种截屏的方式仅限于应用内自己的Activity界面,不需要任何权限,严格来说该方法不属于截屏, ...
- Coherence的集群成员的离开和加入机制研究
最近在客户那里环境中coherence集群不稳定,所以找出一些文档,需要搞清楚Coherence内部的一些机制 1.集群成员的离开 关于状态的检测,官方的说法是: Death detection is ...
- Oracle架构全图
- http://www.oschina.net/question/1019034_153316
http://www.oschina.net/question/1019034_153316 http://www.oschina.net/question/97503_212116?sort=tim ...
- Mybatis学习记录(二)----mybatis开发dao的方法
1 SqlSession使用范围 1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory 将Sq ...