hdu2588 GCD (欧拉函数)
题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数。 (文末有题)
知识点: 欧拉函数。http://www.cnblogs.com/shentr/p/5317442.html
题解一:
当M==1时,显然答案为N。
当M!=1。 X是N的因子的倍数是 gcd(X,N)>1 && X<=N 的充要条件。so 先把N素因子分解,
N=
(e1,e2,…en 从0~ei的全排列包含了所有N的因子。)(可能表达不清,看下面。。)
()中内容相当于:
for(int i=0;i<e1;i++)
for(int j=0;j<e2;j++)
…
for(int k=0;k<en;k++)
x=p1^i*p2^j…pn^k
用dfs解决这个问题,得到所有N的因子。
假设N=p*d,X=q*d.若n与x的最大公约数为d,则能够推出p与q肯定是互质的,因为X<=N所以要求的就是p的欧拉函数值了,那么我们就转化成求满足:N=p*d,并且d>=N的p的欧拉函数值之和了。
如果dfs不是用的很溜的看解法二。
//解法1:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e5; bool vis[N];
int prime[N],cnt;
void is_prime()
{
cnt=0;
memset(vis,0,sizeof(vis));
for(int i=2; i<N; i++)
{
if(!vis[i])
{
prime[cnt++]=i;
for(int j=i+i; j<N; j+=i)
vis[j]=1;
}
}
} int e[100],p[100],cnt2=0;
void fenjie(int n)
{
cnt2=0;
memset(e,0,sizeof(e));
for(int i=0; i<cnt&&prime[i]<=n; i++)
{
if(n%prime[i]==0)
{
p[cnt2]=prime[i];
e[cnt2]++;
n/=prime[i];
while(n%prime[i]==0)
{
n/=prime[i];
e[cnt2]++;
}
cnt2++;
}
}
} int Euler(int n)
{
int ans=n;
for(int i=0; i<cnt&&prime[i]<=n; i++)
{
if(n%prime[i]==0)
{
ans=ans-ans/prime[i];
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n==1)
return ans;
if(n>1)
return ans-ans/n; } LL dfsans[N],cnt3=0;
void dfs(int cur,LL x)
{
if(cur==cnt2)
{
dfsans[cnt3++]=x;
return;
}
for(int i=0;i<=e[cur];i++)
{
LL ans=1;
for(int j=0;j<i;j++)
ans*=p[cur];
dfs(cur+1,x*ans);
}
} int main()
{
int t;
cin>>t;
is_prime();
while(t--)
{
LL n,m;
cin>>n>>m;
fenjie(n);
LL ans=0; cnt3=0;
dfs(0,1);
for(int i=0;i<cnt3;i++)
{
//cout<<dfsans[i]<<endl;
if(dfsans[i]>=m)
ans+=Euler(n/dfsans[i]);
}
cout<<ans<<endl;
}
}
题解二:
只是把dfs换了,其他思路和上面一样。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e5; bool vis[N];
int prime[N],cnt;
void is_prime()
{
cnt=0;
memset(vis,0,sizeof(vis));
for(int i=2;i<N;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
for(int j=i+i;j<N;j+=i)
vis[j]=1;
}
}
} int e[100],p[100],cnt2=0;
void fenjie(int n)
{
cnt2=0;
memset(e,0,sizeof(e));
for(int i=0;i<cnt&&prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
p[cnt2]=prime[i];
e[cnt2]++;
n/=prime[i];
while(n%prime[i]==0)
{
n/=prime[i];
e[cnt2]++;
}
cnt2++;
}
}
} int Euler(int n)
{
int ans=n;
for(int i=0;i<cnt&&prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
ans=ans-ans/prime[i];
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n==1)
return ans;
if(n>1)
return ans-ans/n; } /*LL dfsans[N],cnt3=0;
void dfs(int cur,LL x){ if(cur==cnt2) { dfsans[cnt3++]=x; return; } for(int i=0;i<=e[cur];i++) { LL ans=1; for(int j=0;j<i;j++) ans*=p[cur]; dfs(cur+1,x*ans); } } */ int main()
{
int t;
cin>>t;
is_prime();
while(t--)
{
LL n,m;
cin>>n>>m;
fenjie(n);
LL ans=0;
/*for(int i=0;i<N;i++)
dfsans[i]=1;
cnt3=0;
dfs(0);
for(int i=0;i<cnt3;i++)
{
cout<<dfsans[i]<<endl;
if(dfsans[i]>=m)
ans+=Euler(n/dfsans[i]);
}*/
for(int i=1;i*i<=n;i++)
{
if(n%i==0)
{
if(i>=m)
ans+=Euler(n/i);
if((n/i!=i)&&(n/i>=m))
ans+=Euler(i);
}
}
cout<<ans<<endl;
}
}
GCD
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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,(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.
Input
The 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.
Output
For each test case,output the answer on a single line.
Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
hdu2588 GCD (欧拉函数)的更多相关文章
- 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 ...
- POJ 2773 Happy 2006【GCD/欧拉函数】
根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- Bzoj-2818 Gcd 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(13)-系统日志和异常的处理③
系列目录 上一节我们讲了如何捕获异常和记录日志,这一节我们讲,没有捕获的或者忘记捕获的异常包括404错误等,我们统一处理这个异常. 这一讲是利用 Application_Error 捕获所有异常,全局 ...
- ASP.NET Core中的project.json何去何从?
Shawn Wildermuth (https://wildermuth.com/2016/05/12/The-Future-of-project-json-in-ASP-NET-Core) If y ...
- 2. Struts2 基础
1. Struts2简介 Struts2是一个WEB端MVC框架.作为比较早的MVC 框架之一,Struts2在使用中还是比较多的.虽然个人感受没有SpringMVC还那么的好用 Struts2 官网 ...
- SQL Server索引视图以(物化视图)及索引视图与查询重写
本位出处:http://www.cnblogs.com/wy123/p/6041122.html 经常听Oracle的同学说起来物化视图,物化视图的作用之一就是可以实现查询重写,听起来有一种高大上的感 ...
- PHP之提取多维数组指定列的方法
前言:有时候在开发中会遇到这样的问题,我们需要把有规律的多维数组按照纵向(列)取出,有下面的方法可用: 我们将拿下面的数组来处理: $arr = array( '0' => array('id' ...
- RedisRepository封装—Redis发布订阅以及StackExchange.Redis中的使用
本文版权归博客园和作者本人吴双共同所有,转载请注明本Redis系列分享地址.http://www.cnblogs.com/tdws/tag/NoSql/ Redis Pub/Sub模式 基本介绍 Re ...
- EF里单个实体的增查改删以及主从表关联数据的各种增删 改查
本文目录 EF对单个实体的增查改删 增加单个实体 查询单个实体 修改单个实体 删除单个实体 EF里主从表关联数据的各种增删改查 增加(增加从表数据.增加主从表数据) 查询(根据主表找从表数据.根据从表 ...
- Servlet3.0的可插拔功能
如果说 3.0 版本新增的注解支持是为了简化 Servlet/ 过滤器 / 监听器的声明,从而使得 web.xml 变为可选配置, 那么新增的可插性 (pluggability) 支持则将 Servl ...
- JSON转化为JAVABEAN集合
String str = "[{'id':'392','type':'jpg'},{'id':'393','type':'jpg'},{'id':'377','type':'jpg'}]&q ...
- autofac 组件的实例范围
实例范围决定如何在请求之间共享服务. 原文地址:http://docs.autofac.org/en/latest/lifetime/instance-scope.html 每个依赖一个实例 使用这个 ...