博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU_5514_Frogs
阅读量:5142 次
发布时间:2019-06-13

本文共 3010 字,大约阅读时间需要 10 分钟。

Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2957    Accepted Submission(s): 946

Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 to m1 and the frogs are numbered from 1 to n. The i-th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j+ai) mod m (since all stones lie on a circle).
All frogs start their jump at stone 0, then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away.
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.
 

 

Input
There are multiple test cases (no more than 20), and the first line contains an integer t,
meaning the total number of test cases.
For each test case, the first line contains two positive integer n and m - the number of frogs and stones respectively (1n104, 1m109).
The second line contains n integers a1,a2,,an, where ai denotes step length of the i-th frog (1ai109).
 

 

Output
For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.
 

 

Sample Input
3 2 12 9 10 3 60 22 33 66 9 96 81 40 48 32 64 16 96 42 72
 

 

Sample Output
Case #1: 42 Case #2: 1170 Case #3: 1872
 

 

Source
 
  • 观察对于n和m较小的时候的样例不难发现可以到达的点都是k*gcd(ai,m)<m
  • 但是对于给出的n个a[i]我们不能保证在计算过程中对于同一个点只计算一次
  • 考虑容斥原理解题
  • gcd(ai,m)=di的结果都属于m的因数集合D(m)
  • 如果 u*ai==v*aj 并且 u*ai 属于D(m),那么对于 u*ai的倍数就重复计算了一次,我们就应该在新的计算过程中减去多加的次数
  • 对于多加的次数这个变量的维护可以通过记录当前因数被使用的次数来实现,1-被使用次数就是当前因数对应增量的倍数

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 using namespace std;14 typedef long long LL ;15 typedef unsigned long long ULL ;16 const int maxn = 1e5 + 10 ;17 const int inf = 0x3f3f3f3f ;18 const int npos = -1 ;19 const int mod = 1e9 + 7 ;20 const int mxx = 100 + 5 ;21 const double eps = 1e-6 ;22 const double PI = acos(-1.0) ;23 24 LL gcd(LL x, LL y){25 return y?gcd(y,x%y):x;26 }27 int T, tot;28 LL n, m, a, d, k, ans;29 LL b[maxn], c[maxn], e[maxn];30 int main(){31 // freopen("in.txt","r",stdin);32 // freopen("out.txt","w",stdout);33 while(~scanf("%d",&T)){34 for(int kase=1;kase<=T;kase++){35 ans=0LL;36 tot=0;37 scanf("%lld %lld",&n,&m);38 for(LL i=1LL;i<=(LL)sqrt(m);i++)39 if(0==m%i){40 c[tot++]=i;41 if(i!=m%i)42 c[tot++]=m/i;43 }44 sort(c,c+tot);45 memset(b,0,sizeof(b));46 memset(e,0,sizeof(e));47 for(int i=1;i<=n;i++){48 scanf("%lld",&a);49 d=gcd(a,m);50 b[lower_bound(c,c+tot,d)-c]=1;51 }52 for(int i=0;i

 

 

 

转载于:https://www.cnblogs.com/edward108/p/7642574.html

你可能感兴趣的文章
Kali1.1.0下配置OpenVAS及如何解决相关问题
查看>>
centos 常用命令
查看>>
P1137 旅行计划
查看>>
洛谷 P2212 [USACO14MAR]浇地Watering the Fields
查看>>
umask函数
查看>>
PHP高级笔记汇总
查看>>
cxGrid用法-最新
查看>>
如何在SqlServer中获取前端连接的IP地址,计算机名等信息
查看>>
webpack.optimize.CommonsChunkPlugin插件的使用
查看>>
在VS2010中配制Elmah邮件发送到Gmail
查看>>
变量的范围 namespace
查看>>
队列-生产者消费者模式
查看>>
学习笔记23_AspMVC项目
查看>>
webstrom提示不见了
查看>>
Linux 永久挂载镜像文件和制作yum源
查看>>
Lock和synchronized比较详解(转)
查看>>
eclipse代码编辑器中按alt+/提示No Default Proposals 的解决方法
查看>>
相似算法-编辑距离
查看>>
hql date比较
查看>>
前端如何判断音视频是否播放完毕
查看>>