Frogs

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

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
  1. /*
  2. hdu 5514 Frogs(容斥)
  3.  
  4. problem:
  5. 有n只青蛙和围成一个圈的m个石头. 每只青蛙可以跳a[i]步. 求所有被占领过的石头的编号和
  6.  
  7. solve:
  8. 可以发现青蛙会经过 GCD(a[i],m)的倍数的点.但是有很多重复跳过的石头. 所以会想到容斥.
  9. 如果暴力肯定要GG. 可以发现青蛙只会经过gcd(x,m)的点,也就是m的因子.
  10. 所以可以把枚举m改成枚举m的因子. 然后利用容斥减去重复的就行.
  11.  
  12. hhh-2016-08-30 16:30:55
  13. */
  14. #pragma comment(linker,"/STACK:124000000,124000000")
  15. #include <algorithm>
  16. #include <iostream>
  17. #include <cstdlib>
  18. #include <cstdio>
  19. #include <cstring>
  20. #include <vector>
  21. #include <math.h>
  22. #include <queue>
  23. #include <set>
  24. #include <map>
  25. #define lson i<<1
  26. #define rson i<<1|1
  27. #define ll long long
  28. #define clr(a,b) memset(a,b,sizeof(a))
  29. #define scanfi(a) scanf("%d",&a)
  30. #define scanfs(a) scanf("%s",a)
  31. #define scanfl(a) scanf("%I64d",&a)
  32. #define key_val ch[ch[root][1]][0]
  33. #define inf 1e9
  34. using namespace std;
  35. const ll mod = 1e9+7;
  36. const int maxn = 20005;
  37. int fac[maxn];
  38. int fcnt;
  39. int vis[maxn],hac[maxn];
  40.  
  41. int gcd(int a,int b)
  42. {
  43. return b ? gcd(b,a%b) : a;
  44. }
  45.  
  46. int main()
  47. {
  48. int T,n,m;
  49. // freopen("in.txt","r",stdin);
  50. scanfi(T);
  51. int cas = 1;
  52. while(T--)
  53. {
  54. scanfi(n),scanfi(m);
  55. fcnt = 0;
  56. for(int i = 1;i*i <= m;i++)
  57. {
  58. if(m % i == 0)
  59. {
  60. fac[fcnt++] = i;
  61. if(i *i != m)
  62. fac[fcnt ++ ] = m/i;
  63. }
  64. }
  65. sort(fac,fac+fcnt);
  66. // for(int i = 0;i < fcnt;i++)
  67. // cout << fac[i] <<" ";
  68. // cout <<endl;
  69. int x;
  70. clr(vis,0),clr(hac,0);
  71. for(int i = 1;i <= n;i++) //处理会经过哪些因子
  72. {
  73. scanfi(x);
  74. x = gcd(x,m);
  75. for(int j = 0;j < fcnt;j++)
  76. {
  77. if(fac[j] % x== 0)
  78. vis[j] = 1;
  79. }
  80. }
  81. vis[fcnt-1] = 0;
  82. ll ans = 0;
  83. for(int i = 0;i < fcnt;i++)
  84. {
  85. if(vis[i] != hac[i])
  86. {
  87. int t = (m-1)/fac[i];
  88. ans += (ll)t*(t+1)/2 * fac[i] * (vis[i] - hac[i]); //容斥原理. vis应该计算次数,hac已经计算次数
  89. t = (vis[i] - hac[i]);
  90. for(int j = 0;j < fcnt;j++)
  91. {
  92. if(fac[j] % fac[i] == 0)
  93. hac[j] += t;
  94. }
  95. }
  96. }
  97. printf("Case #%d: %I64d\n",cas++,ans);
  98. }
  99. return 0;
  100. }

  

hdu 5514 Frogs(容斥)的更多相关文章

  1. HDU 5514 Frogs 容斥定理

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

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

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

  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 Frogs

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

  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 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

随机推荐

  1. 实验四Java Android简易开发

    实验准备 Android Studio下载 Android Studio安装 实验内容 Android Stuidio的安装测试 Android Stuidio的安装测试: 参考<Java和An ...

  2. Vim 中文社区:期待你的加入

    我们的愿景 Vim 中文社区一直比较零散,缺少凝聚力,现有的一些群经常也是水的可以的,讨论各种无关紧要的内容,于是导致很大一部分人,将这些群丢入了群助手,渐渐地他们也淡出了 vim 中文社区. 而我理 ...

  3. nyoj 矩形个数

    矩形的个数 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3 ...

  4. mysql5.5中datetime默认值不能为NOW或者CURRENT_TIMESTAMP,用触发器解决

    mysql5.6及以上的版本datatime默认值可以为CURRENT_TIMESTAMP或者NOW 那我们要用的是mysql5.5及以下版本呢? 请看代码 delimiter // DROP TRI ...

  5. netty : NioEventLoopGroup 源码分析

    NioEventLoopGroup 源码分析 1. 在阅读源码时做了一定的注释,并且做了一些测试分析源码内的执行流程,由于博客篇幅有限.为了方便 IDE 查看.跟踪.调试 代码,所以在 github ...

  6. Python内置函数(19)——oct

    英文文档: oct(x) Convert an integer number to an octal string. The result is a valid Python expression. ...

  7. 4-51单片机WIFI学习(开发板51单片机自动冷启动下载原理)

    上一篇链接 http://www.cnblogs.com/yangfengwu/p/8743936.html 这一篇说一下自己板子的51单片机自动冷启动下载原理,我挥舞着键盘和鼠标,发誓要把世界写个明 ...

  8. jhipster生成项目无法使用restful请求,报access_denied 403错误

    写在前边: 我们的微服务是注册中心.uaa.gateway为基础,添加微服务应用,昨天下午在测试jhipster的增删改查,因为jhipster生成的代码都是restful的,好不容易找到网关配置的映 ...

  9. 详解Ajax请求(四)——多个异步请求的执行顺序

    首先提出一个问题:点击页面上一个按钮发送两个ajax请求,其中一个请求会不会等待另一个请求执行完毕之后再执行? 答案是:不会,这两个异步请求会同时发送,至于执行的快与慢,要看响应的数据量的大小及后台逻 ...

  10. JavaScript 重点笔记

    JavaScript 重点笔记 ## 数组 // 必须掌握 - arr.length:获取数组元素的长度 - arr.splice(起始位置,长度):从数组中添加或删除元素. - arr.indexO ...