F - Frogs  HDU - 5514

There are mm stones lying on a circle, and nn frogs are jumping over them. 
The stones are numbered from 00 to m−1m−1 and the frogs are numbered from 11 to nn. The ii-th frog can jump over exactly aiai stones in a single step, which means from stone j mod mj mod m to stone (j+ai) mod m(j+ai) mod m (since all stones lie on a circle).

All frogs start their jump at stone 00, 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 2020), and the first line contains an integer tt, 
meaning the total number of test cases.

For each test case, the first line contains two positive integer nn and mm - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109)(1≤n≤104, 1≤m≤109).

The second line contains nn integers a1,a2,⋯,ana1,a2,⋯,an, where aiai denotes step length of the ii-th frog (1≤ai≤109)(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

题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子。

问所有被至少踩过一次的石子的序号之和。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
ll fac[maxn],vis[maxn],num[maxn];int main()
{int T,cas=0;scanf("%d",&T);while(T--){ll ans=0,m,x;int n,tot=0;scanf("%d%lld",&n,&m);memset(vis,0,sizeof(vis)); //m的因数%a[i]==0memset(num,0,sizeof(num));for(ll i=1;i*i<=m;i++){if(m%i==0){fac[++tot]=i;if(i*i!=m)fac[++tot]=m/i;}}sort(fac+1,fac+tot+1);for(int i=1;i<=n;i++){scanf("%lld",&x);ll p=__gcd(x,m);for(int j=1;j<tot;j++){if(fac[j]%p==0)vis[j]=1;}}for(int i=1;i<tot;i++){if(num[i]!=vis[i]){ll t=(m-1)/fac[i];ans+=t*(t+1)/2*fac[i]*(vis[i]-num[i]); //重复加减掉for(int j=i+1;j<tot;j++){if(fac[j]%fac[i]==0){num[j]+=vis[i]-num[i];  //哪些因数被重复加了几次}}}}printf("Case #%d: %lld\n",++cas,ans);}return 0;
}

【容斥好题】HDU - 5514 F - Frogs相关推荐

  1. 吉首大学新星杯L题 小李子的老年生活【容斥好题(只是问题比较裸)】

    传送门 // 题意就是给定一个n, 求1 - n 中与n互质的数的平方和. // 思路: 问题我们可以进行转换, 就是我们得到1 - n的所有数的平方和, 然后我们要做的就是减去n的素数因子(不是素数 ...

  2. 【洛谷】1600:天天爱跑步【LCA】【开桶】【容斥】【推式子】

    P1600 天天爱跑步 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个 ...

  3. 【BZOJ4710】[JSOI2011]分特产(容斥)

    [BZOJ4710]分特产(容斥) 题面 BZOJ 题解 比较简单吧... 设\(f[i]\)表示至多有\(i\)个人拿到东西的方案数. \(f[i]=\prod_{j=1}^m C_{m+i-1}^ ...

  4. 【BZOJ4833】最小公倍佩尔数(min-max容斥)

    [BZOJ4833]最小公倍佩尔数(min-max容斥) 题面 BZOJ 题解 首先考虑怎么求\(f(n)\),考虑递推这个东西 \((1+\sqrt 2)(e(n-1)+f(n-1)\sqrt 2) ...

  5. 【LOJ#6072】苹果树(矩阵树定理,折半搜索,容斥)

    [LOJ#6072]苹果树(矩阵树定理,折半搜索,容斥) 题面 LOJ 题解 emmmm,这题似乎猫讲过一次... 显然先\(meet-in-the-middle\)搜索一下对于每个有用的苹果数量,满 ...

  6. HDU 5514 Frogs 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=5514 没有想到怎么利用gcd的性质来改进指数级别的容斥,还是没有理解容斥的思想,只知道基础的. 已经知道结果就在 ...

  7. hdu 5514 容斥

    hdu 5514 题意:有n个青蛙编号1~n,m块石头编号0~m-1,石头围成一圈,每只青蛙会从第ii块跳到第(i + ai) mod m(i \ + \ a_i) \ mod \ m块,青蛙会跳无数 ...

  8. Frogs HDU - 5514

    Frogs HDU - 5514 题意: 有n个青蛙,第 i 个青蛙每次只能够跳 ai​步,现在有m个石头围成一圈,编号为0到m−1,现在青蛙可以围着这个石头组成的圆跳无限次,每跳一次就会占领这个石头 ...

  9. HDU 6143 Killer Names(排列+容斥,dp)

    Killer Names HDU 6143 (容斥+排列组合,dp+整数快速幂) 2017ACM暑期多校联合训练 - Team 8 1011 Killer Names 题目链接 Time Limit: ...

  10. HDU - 7009 树上游走(树的直径+容斥)

    题目链接:点击查看 题目大意:给一棵树,称一个点集 S 是好的当且仅当存在一个点,其到 S 中所有点的距离互不相同,求 |S| 的最大值和使得 |S| 最大的 S 的个数 题目分析:不难看出 ∣S∣| ...

最新文章

  1. L2-002. 链表去重
  2. 人工智能热潮下,我们该如何紧跟科技脚步呢?
  3. python画蝴蝶_python画蝴蝶曲线图的实例
  4. 揭秘Kaggle神器xgboost
  5. STM32的SPI时钟
  6. android settings源代码分析(2)
  7. boost::contract模块实现非成员函数的测试程序
  8. 空间谱专题02:波束形成(Beamforming)
  9. 信息奥赛一本通(1112:最大值和最小值的差)
  10. Verilog 常规数据定义
  11. Win10相机打不开提示:我们找不到你的相机 错误代码0xa00f4244!
  12. html动态添加删除文本框,DOM动态添加与删除元素
  13. 如何在Java中实现画图(图文并茂)(内附美丽的分型图片哦)
  14. KeyShot 11.1 现已推出
  15. 手把手教你用MATLAB制作一款 [狗头翻牌子] 小游戏(点灯游戏)
  16. Surciata源码分析之IpsNFQ模式(1)
  17. pthread_cancel 线程阻塞问题
  18. Java实现上传(支持多个文件同时上传)和下载
  19. DPad down是哪个键_上海crm系统哪个好_仓库erp管理
  20. AI翻译新思路,OBTranslate打破非洲城乡居民之间的沟通差距

热门文章

  1. 入股不亏的文案编辑工具
  2. 设置win7一直以管理员身份运行
  3. 微软发布Windows 10:连Windows 7都能免费升级了
  4. jstl和el表达式
  5. python中三元运算符_Python中三元表达式的几种写法介绍
  6. Python综合知识随笔
  7. Win10 如何在系统内用cmd命令查看系统详细信息
  8. Linux Ubantu 关于vi中上下左右键和Backspace键无法使用解决
  9. [35期] 没有硝烟的战争
  10. java.lang.IllegalArgumentException: Receiver not registered