从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)
(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.
InputThe 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.OutputFor each test case,output the answer on a single line.Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
题意:求有多少个1<=X<=N,满足gcd(X,N)>=M。
思路:即求Σd=gcd(X,N)>=M;枚举d,而d是M的因子,不超过根号N个;对枚举的d,用欧拉公式求得有多少个X满足gcd(X,N)=d;
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int p[maxn+],vis[maxn+],phi[maxn],cnt;
void getprime()
{
phi[]=;
for(int i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt&&i*p[j]<=maxn;j++){
vis[i*p[j]]=; phi[i*p[j]]=phi[i]*(p[j]-);
if(i%p[j]==){
phi[i*p[j]]=phi[i]*p[j];
break;
}
}
}
}
int tot,ans,fac[maxn];
void divide(int x)
{
for(int i=;i*i<=x;i++){
if(x%i==){
fac[++tot]=i;
if(i*i!=x) fac[++tot]=x/i;
}
}
}
map<int,int>PHI;
int getphi(int x)
{
if(x<=maxn) return phi[x];
if(PHI[x]) return PHI[x];
int res=x;
for(int i=;i*i<=x;i++)
if(x%i==) {
res=res/i*(i-);
while(x%i==) x/=i;
}
if(x>) res=res/x*(x-);
PHI[x]=res;
return res;
}
int main()
{
getprime();
int T,N,M,i,j;
scanf("%d",&T);
while(T--){
tot=ans=;
scanf("%d%d",&N,&M);
divide(N);
for(i=;i<=tot;i++){
if(N/fac[i]>=M) ans+=getphi(fac[i]);
}
printf("%d\n",ans);
}
return ;
}
---------------------------------分界线--------------------------------
再来看HDU5514。。。(虽然复杂度看似有些巧合)。
只要枚举M的因子,然后验证如果是属于某个gcd(a,M)的倍数,就可以累加其所到位子,求和的时候利用对称性,有公式Σ=φ(x)*m/2;。
(容斥定理也可以做,但是我想不出来)。。。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
int p[maxn+],vis[maxn+],phi[maxn],cnt;
void getprime()
{
phi[]=; phi[]=;
for(int i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt&&i*p[j]<=maxn;j++){
vis[i*p[j]]=; phi[i*p[j]]=phi[i]*(p[j]-);
if(i%p[j]==){
phi[i*p[j]]=phi[i]*p[j];
break;
}
}
}
}
int tot,fac[maxn];
void divide(int x)
{
for(int i=;i*i<=x;i++){
if(x%i==){
fac[++tot]=i;
if(i*i!=x) fac[++tot]=x/i;
}
}
}
int getphi(int x)
{
if(x<=maxn) return phi[x];
int res=x;
for(int i=;i*i<=x;i++)
if(x%i==) {
res=res/i*(i-);
while(x%i==) x/=i;
}
if(x>) res=res/x*(x-);
return res;
}
int a[maxn],N,M;
bool check(int x)
{
for(int i=;i<=N;i++)
if(x%a[i]==) return true;
return false;
}
int main()
{
getprime();
int T,Case=,i; ll ans;
scanf("%d",&T);
while(T--){
tot=ans=;
scanf("%d%d",&N,&M);
for(i=;i<=N;i++){
scanf("%d",&a[i]);
a[i]=__gcd(a[i],M);
}
divide(M);
for(i=;i<=tot;i++){
if(fac[i]!=M&&check(fac[i])) ans+=(ll)getphi(M/fac[i])*M/;
}
printf("Case #%d: %lld\n",++Case, ans);
}
return ;
}
从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)的更多相关文章
- HDU5514 Frogs
/* HDU5514 Frogs http://acm.hdu.edu.cn/showproblem.php?pid=5514 容斥原理 * * */ #include <cstdio> ...
- hdu2588 GCD (欧拉函数)
GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数. (文末有题) 知 ...
- HDU2588 GCD(欧拉函数)
题目问[1,n]中与n的gcd大于等于m的数的个数. 好难想... 假设x满足条件,那么gcd(x,n)=d>=m,而x/d与n/d一定互质. 又x<=n,所以x/d<=n/d. 于 ...
- hdu2588 gcd 欧拉函数
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU2588:GCD(欧拉函数的应用)
题目链接:传送门 题目需求:Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.(2& ...
- hdu2588 GCD
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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 Sub ...
- 【做题】hdu5514 Frogs——另类容斥
题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...
- hdu 5514 Frogs 容斥思想+gcd 银牌题
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
随机推荐
- Sparsity稀疏编码(三)
稀疏编码(sparse coding)和低秩矩阵(low rank)的区别 上两个小结介绍了稀疏编码的生命科学解释,也给出一些稀疏编码模型的原型(比如LASSO),稀疏编码之前的探讨文章 ...
- AISing Programming Contest 2019 Solution
A - Bulletin Board 签到. #include <bits/stdc++.h> using namespace std; int main() { int n, h, w; ...
- Ubuntu软件包管理器
Ubuntu软件包管理 Ubuntu下对软件管理工具有:apt,dpkg,tasksel,aptitude等,我们常用的就是前三个工具.下面就介绍这三个工具的用法. dpkg 在Linux发展之初,安 ...
- 【运维技术】Jenkins配置使用教程
Jenkins配置使用教程 单机jenkins启动 软件安装和启动,必须含有java环境 # 安装jdk,参考其他教程,创建文件目录 mkdir -p /app/jenkins cd /app/jen ...
- sudo pm2 提示 找不到命令
一共有 3 台 staging 环境服务器,5 台生产环境服务器,centos7系统.前阵子刚给所有研发配置了自己的账号,今天有小伙伴说,在其中两台服务器上 sudo pm2 list 提示: sud ...
- Java序列化(Serialization)
关于Java的序列化的文章在网上已经够多了,在这里写关于Java序列化的文章是对自己关于这方面的的一种总结,结合以前的开发经验与网上的资料,写了这篇文章,对自己是有着巩固记忆的作用,也希望能够对大家有 ...
- Linux下查找大文件,大目录的方法
查找大文件 //列举出当前目录所有大于800M的文件 find . -type f -size +800M 1 2 第一个方法只用到了一个命令find,它能够帮我们做一些文件查找的操作.它常用的参数有 ...
- hyper-v安装虚拟机ubuntu 18.04 64bit后无法使能增强模式怎么办
1.获取脚本来使能增强模式 $ sudo apt-get update $ sudo apt install git $ git clone https://github.com/jterry75/x ...
- artTemplate 模板使用
下载github中文件,浏览器引用lib/template-web.js 模板html: {{each ProductInfoList as prd}} <div class="res ...
- apache配置文件详解与优化
apache配置文件详解与优化 一.总结 一句话总结:结合apache配置文件中的英文说明和配置详解一起看 1.apache模块配置用的什么标签? IfModule 例如: <IfModule ...