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,(1,2)=1,(12,18)=6. 
 (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 (欧拉公式)的更多相关文章

  1. HDU5514 Frogs

    /* HDU5514 Frogs http://acm.hdu.edu.cn/showproblem.php?pid=5514 容斥原理 * * */ #include <cstdio> ...

  2. hdu2588 GCD (欧拉函数)

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

  3. HDU2588 GCD(欧拉函数)

    题目问[1,n]中与n的gcd大于等于m的数的个数. 好难想... 假设x满足条件,那么gcd(x,n)=d>=m,而x/d与n/d一定互质. 又x<=n,所以x/d<=n/d. 于 ...

  4. hdu2588 gcd 欧拉函数

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

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

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

  6. hdu2588 GCD

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

  7. 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 ...

  8. 【做题】hdu5514 Frogs——另类容斥

    题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...

  9. hdu 5514 Frogs 容斥思想+gcd 银牌题

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

随机推荐

  1. Java基础知识陷阱(十)

    本文发表于本人博客. 上个星期由于时间比较紧所以未能继续写下去,今天再接再厉,专心 + 坚持这样离目标就越来越近了!废话少说说正题,今天我们还是来说说java中比较基础的知识,大家知道编写java程序 ...

  2. VS异常--未找到与约束 ContractName Microsoft.VisualStudio.Language.Intellisense.IGlyphService RequiredTypeIdentity

    早上打开项目的时候突然遇到这么个错误: ======================= 未找到与约束 ContractName Microsoft.VisualStudio.Language.Inte ...

  3. nginx重启 failed (98: Address already in use)

    启动nginx服务,无法正常启动,一查log日志,发现如题错误信息. 问题描述:地址已被使用.可能nginx服务卡死了,导致端口占用,出现此错误. 查看端口 netstat -ntpl 杀掉进程   ...

  4. Vue学习笔记之Vue的使用

    0x00 安装 对于新手来说,强烈建议大家使用<script>引入 0x01 引入vue.js文件 我们能发现,引入vue.js文件之后,Vue被注册为一个全局的变量,它是一个构造函数. ...

  5. SQL学习笔记四(补充-2-1)之MySQL SQL查询作业答案

    阅读目录 一 题目 二 答案 一 题目 1.查询所有的课程的名称以及对应的任课老师姓名 2.查询学生表中男女生各有多少人 3.查询物理成绩等于100的学生的姓名 4.查询平均成绩大于八十分的同学的姓名 ...

  6. PHP多进程学习(三)__代码案例来了解父进程与子进程的执行顺序

    pcntl_fork创建子进程成功的话,系统就有了2个进程,一个为父进程,一个为子进程,父进程和子进程都继续向下执行,子进程的id号为$pid(父进程会获取子进程的$pid也就是$pid不为0,而子进 ...

  7. 关于《Java读书笔记》第六章课后习题选择题总结与疑问

    课后习题 选择题 3 题 代码: class Some{ String ToString(){ return "Some instance"; } } public class M ...

  8. Mac下将C程序创建为动态链接库再由另一个C程序调用

    写C的时候需要调用之前的一个C程序,想用动态链接库的方式.Mac下的动态链接库是dylib,与Linux下的.os或Windows下的.dll不同.由于之前没有接触过,所以翻了大量的博客,然而在编译过 ...

  9. Jwt访问api提示401错误 Authorization has been denied for this request

    教程  http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-ap ...

  10. 使用ntpdate工具校正linux服务器时间

    当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间. 安装:yum install ntpdate ntpdate简单用法: # ntpdate ip # ntpdate 210 ...