HDU 5514 欧拉函数应用
前置技能:
<=i且与i互质的数的和是phi(i)*i/2
思路:
显然每个人的步数是gcd(a[i],m)
把m的所有因数预处理出来
1~m-1中的每个数 只会被gcd(m,i)筛掉一遍
//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=;
typedef long long ll;
int cases,n,m,a[N],s[N],tp,I=;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int get_phi(int x){
int t=x;
for(int i=;i*i<=x;i++)if(x%i==){
t-=t/i;
while(x%i==)x/=i;
}if(x>)t-=t/x;
return t;
}
int main(){
for(scanf("%d",&cases);I<=cases;I++){
long long ans=;tp=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]),a[i]=gcd(a[i],m);
sort(a+,a++n);
n=unique(a+,a++n)-a-;
for(int i=;i*i<=m;i++)if(m%i==){
if(i*i==m)s[++tp]=i;
else{
s[++tp]=i;
if(i!=)s[++tp]=m/i;
}
}
for(int i=;i<=tp;i++)
for(int j=;j<=n;j++)if(s[i]%a[j]==){
ans+=get_phi(m/s[i]);break;
}
printf("Case #%d: %lld\n",I,1ll*ans*m/);
}
}
HDU 5514 欧拉函数应用的更多相关文章
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- HDU 5514.Frogs-欧拉函数 or 容斥原理
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 2654(欧拉函数)
Become A Hero Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2824(欧拉函数)
The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1395(欧拉函数)
2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 3307(欧拉函数+好题)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- 找新朋友 HDU - 1286 欧拉函数模板题
题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...
- hdu 2824 欧拉函数 O(nlogn) 和O(n)
裸题 O(nlogn): #include <cstdio> #include <iostream> #include <algorithm> using name ...
- hdu 4983 欧拉函数
http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...
随机推荐
- 【NOIP2017练习】怎样更有力气(二分答案,线性扫描)
题意:OI大师抖儿在夺得银牌之后,顺利保送pku.这一天,抖儿问长者:“我虽然已经保送了,但我的志向是为国家健康工作五十年.请问我应该怎样变得更有力气?” 长者回答:“你啊,Too Young T ...
- 3.2 符号表之二叉查找树BST
一.插入和查找 1.二叉查找树(Binary Search Tree)是一棵二叉树,并且每个结点都含有一个Comparable的键,保证每个结点的键都大于其左子树中任意结点的键而小于其右子树的任意结点 ...
- codevs3285 转圈游戏
题目描述 Description n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 ...
- PatentTips - Register file supporting transactional processing
BACKGROUND OF THE INVENTION With the rise of multi-core, multi-threaded data processing systems, a k ...
- Nginx 的 server_names_hash_bucket_size 问题
在 Nginx 0.6.35 的版本中,配置多个 server 虚拟主机,必须要在配置文档中 http { 里头加上 server_names_hash_bucket_size 64; 这么一句 ht ...
- Thinkphp5.0 的视图view的循环标签
Thinkphp5.0 的视图view的循环标签 volist标签: <!-- 使用volist --> <!-- name是传递过来的要循环变量名 --> <!-- k ...
- laravel有用的方法
1.tinker 造假数据 factory('App\User',3)->create(); DB::table 返回collection,可以用collection中的很多方法 比如-> ...
- Django学习系列之django restframework
曾几何时,Ajax已经统治了Web开发中的客户端,而REST成为web世界中最流行的架构风格(architecture style).所以我们的选择变得很简单:前端ajax访问后端的RESTful A ...
- 请问这个「 (?<=<(\w+)>).*(?=<\/\1>) 」正则表达式是什么意思呢?
问题:https://www.zhihu.com/question/26480812 (?<=<(\w+)>).*(?=<\/\1>) ---------------- ...
- CentOS command
管理centos服务器的时候常会对文件进行一些常规操作,除了ftp之外了解在ssh下必要的文件操作命令那也是必不可少的,以下摘录一些常用的文件操作命令: 文件操作: ls ####查看目录中的文件## ...