HDU 5514 Frogs（容斥原理）——2015ACM/ICPC亚洲区沈阳站-重现赛（感谢东北大学）
2017-02-11       个评论    来源： ITAK
我要投稿

HDU 5514 Frogs（容斥原理）——2015ACM/ICPC亚洲区沈阳站-重现赛。

# Frogs

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

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 jmodm to stone (j+ai)modm (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

n(n≤104) 个青蛙和 m(m≤109) 个石头，石头编号从 0?n?1i 个青蛙能够跳 ai(ai≤109) 步，青蛙可以跳无限次，现在问你的是这 n 个青蛙跳过的个石头的编号的总和是多少。

MyCode：

```/**
2016 - 10 - 02 晚上
Author: ITAK

Motto:

**/

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1e17+5;
const LL MAXN = 1e6+5;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;

LL a[MAXN], tmp[MAXN];
LL vis[MAXN], num[MAXN];///vis:标记可以走哪些因子数 num[i]:第i个因子加了几次。
LL GCD(LL a, LL b){
if(b == 0)
return a;
return GCD(b, a%b);
}
int main()
{
LL T;
scanf("%lld",&T);
for(LL cas=1; cas<=T; cas++){
LL n, m, cnt = 0;
scanf("%lld%lld",&n,&m);
for(LL i=1; i*i<=m; i++){
if(m%i==0){
if(i*i != m)
tmp[cnt++] = m/i;
tmp[cnt++] = i;
}
}
sort(tmp, tmp+cnt);
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
for(LL i=0; i

```