Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1509    Accepted Submission(s): 498

Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from 0 to m−1 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 (1≤n≤104, 1≤m≤109).

The second line contains n integers a1,a2,⋯,an, where ai denotes step length of the i-th frog (1≤ai≤109).

 
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个石头(围成圆圈)。第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少?
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <map>
  8. #include <bitset>
  9. #include <stack>
  10. #include <queue>
  11. #include <vector>
  12. #include <bitset>
  13. #include <set>
  14. #define MM(a,b) memset(a,b,sizeof(a));
  15. #define inf 0x3f3f3f3f
  16. using namespace std;
  17. typedef long long ll;
  18. #define CT continue
  19. #define SC scanf
  20. const int N=2*1e5+10;
  21. int factor[N],num[N],appear[N],step[N];
  22. ll add[N];
  23.  
  24. int gcd(int a,int b)
  25. {
  26. if(b==0) return a;
  27. else return gcd(b,a%b);
  28. }
  29.  
  30. int main()
  31. {
  32. int cas,n,m,kk=0;
  33. SC("%d",&cas);
  34. while(cas--){
  35. SC("%d%d",&n,&m);
  36. int cnt=0;MM(num,0);MM(appear,0);
  37. for(int i=1;i<=n;i++) SC("%d",&step[i]);
  38. for(int i=1;i*i<=m;i++) if(m%i==0){
  39. factor[++cnt]=i;
  40. if(i*i!=m) factor[++cnt]=m/i;
  41. }
  42. sort(factor+1,factor+cnt+1);
  43. cnt--;
  44. for(int i=1;i<=cnt;i++){
  45. ll k=(m-1)/factor[i];
  46. add[i]=k*(k+1)/2*factor[i];
  47. }
  48.  
  49. for(int i=1;i<=n;i++){
  50. int k=gcd(step[i],m);
  51. for(int j=1;j<=cnt;j++)
  52. if(factor[j]%k==0) appear[j]=1;
  53. }
  54.  
  55. ll ans=0;
  56. for(int i=1;i<=cnt;i++) if(num[i]!=appear[i]){
  57. ans+=add[i]*(appear[i]-num[i]);
  58. for(int j=i+1;j<=cnt;j++)
  59. if(factor[j]%factor[i]==0) num[j]+=(appear[i]-num[i]);
  60. }
  61. printf("Case #%d: %lld\n",++kk,ans);
  62. }
  63. return 0;
  64. }

  分析:

1.一个数的因子个数大概是log级别;

2.ax+by=c有非负整数解的条件是c%gcd(a,b);取余

=>ax+by=k*gcd(a,b) =>ax%b=k*gcd(a,b)%b =>ai*x%m=k*gcd(ai,m)%m;

所以,青蛙能走到的格子数是k*gcd(ai,m),而gcd(ai,m)又必然是m的因数,所以可以先分解出m的因子

3.但是因为有些格子可能同时被多只青蛙走,因此需要容斥一下,设appear[i]为格子i应该走的次数(只有0,1)两个值,num[i]为格子i到当前为止实际走的次数,如果当前appear[i]>appear[i],就需要加,否则减去,然后将是当前因子factor[i]倍数的因子也同时跟新num值

hdu 5514 Frogs 容斥思想+gcd 银牌题的更多相关文章

  1. hdu 5514 Frogs(容斥)

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

  2. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  3. ACM-ICPC 2015 沈阳赛区现场赛 F. Frogs && HDU 5514(容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子.问所 ...

  4. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  5. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

  6. 很好的容斥思想 HDU 5514

    题目描述:有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条a[i]个石头,问最后所有青蛙跳过的石头的下标总和是多少? 思路:经过绘图我们发现,每次跳过的位置一定是k*gcd(a[i], m).然 ...

  7. HDU 5514 Frogs (容斥原理)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意 : 有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过a[i] ...

  8. HDU 5514 Frogs(容斥原理)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5514 [题目大意] m个石子围成一圈,标号为0~m-1,现在有n只青蛙,每只每次跳a[i]个石子, ...

  9. HDU 5514 Frogs

    Frogs Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5514 ...

随机推荐

  1. MySQL_Utilities工具

    需求    Python 2.6    MySQL Connector/Python 连接器 下载地址:    http://dev.mysql.com/downloads/utilities/   ...

  2. Python 异常处理与反射机制

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  3. k8s之dashboard认证、资源需求、资源限制及HeapSter

    1.部署dashboard kubernetes-dashboard运行时需要有sa账号提供权限 Dashboard官方地址:https://github.com/kubernetes/dashboa ...

  4. JS中json数组多字段排序方法(解决兼容性问题)(转)

    前端对一个json数组进行排序,用户需要动态的根据自己的选择来对json数据进行排序. 由于后台表设计问题所以不能用sql进行排序,这里用到了js的sort方法. 如果对单字段排序,那么很简单,一个s ...

  5. C++反汇编第四讲,认识多重继承,菱形继承的内存结构,以及反汇编中的表现形式.

    目录: 1.多重继承在内存中的表现形式 多重继承在汇编中的表现形式 2.菱形继承 普通的菱形继承 虚继承 汇编中的表现形式 一丶多重继承在内存中的表现形式 高级代码: class Father1 { ...

  6. hdu 3371 有毒的卡时间题目

    同样的代码 每次交的结果都不一样 #include<stdio.h> #include<string.h> #include<stdlib.h> #include& ...

  7. IP 、127.0.0.1、localhost 三者区别

    一.Ping命令 1.Ping命令,用来检查两台物理机间的TCP/IP网络是否通畅或者网络连接速度,是TCP/IP协议的一部分. 2.PING (Packet Internet Groper),因特网 ...

  8. BPM软件_K2再度入选Gartner iBPMS MQ挑战者象限_全球领先的工作流引擎

    在Gartner 于1月最新发布的2018 iBPMS MQ报告中,K2再度入选“挑战者”象限,相较去年,K2在“前瞻性”方面有了显著提升. Gartner对该标准的定义为:供应商对市场具有清晰认识, ...

  9. Python-memcached的使用用法

    Memcached API set(key,val,time=0,min_compress_len=0) 无条件键值对的设置,其中的time用于设置超时,单位是秒,而min_compress_len则 ...

  10. MySQL cmd操作

    1.开启关闭服务 net start mysql net stio mysql 2.登陆 在CMD命令窗口敲入命令 mysql -hlocalhost -uroot -p 后按回车(注意这里的&quo ...