HDU 5514 Frogs
2015-11-09 19:00
423 查看
Frogs
Time Limit: 1000msMemory Limit: 65536KB
This problem will be judged on HDU. Original ID: 5514
64-bit integer IO format: %I64d Java class name: Main
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
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)解题:容斥来一发即可
#include <bits/stdc++.h> using namespace std; const int maxn = 10010; using LL = long long; int n,m; LL ret,a[maxn]; void dfs(int pos,LL lcm,int cnt) { if(pos == n) { if(cnt) { LL num = m/lcm,tmp = (((num - 1)*num)>>1)*lcm; ret += (cnt&1)?tmp:-tmp; } return; } if(lcm%a[pos] == 0) return; dfs(pos + 1,lcm,cnt); LL LCM = lcm*a[pos]/__gcd(lcm,a[pos]); if(LCM < m) dfs(pos + 1,LCM,cnt + 1); } int main() { int kase,cs = 1; scanf("%d",&kase); while(kase--) { scanf("%d%d",&n,&m); for(int i = 0; i < n; ++i) { scanf("%I64d",a + i); a[i] = __gcd(a[i],static_cast<LL>(m)); } sort(a, a + n); n = unique(a,a + n) - a; ret = 0; if(a[0] == 1) ret = (static_cast<LL>(m)*(m-1))>>1; else dfs(0,1,0); printf("Case #%d: %I64d\n",cs++,ret); } return 0; }
View Code
相关文章推荐
- Java内存回收机制(第四讲)
- session, cookie
- mysql 。。。
- Linux 对 ARM 的支持一团糟
- 数据结构之最短路径Dijkdtra算法
- apache kafka监控系列-KafkaOffsetMonitor(转)
- acm沈阳站
- python实现zabbix批量监控WEB网站和批量监控主机端口
- 大数据架构之:流式计算数据流向
- 随机数的循环
- LeetCode Basic Calculator 栈
- JAVA基础复习03
- JAVA基础复习03
- 角谷猜想(C程序设计进阶第3周)
- Introduction to Algorithm - Summary of Chapter 6 - Heapsort
- easyui-combobox 取值问题
- BroadcastReceiver广播传递信息
- Makefile中的“= := ?=”
- 正确使用Block避免Cycle Retain和Crash
- 大道至简读后感---从编程到工程