HDU 5514 Frogs

Frogs

Time Limit: 1000ms

Memory 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亚洲区沈阳站-重现赛(感谢东北大学)

解题:容斥来一发即可

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 10010;
 4 using LL = long long;
 5 int n,m;
 6 LL ret,a[maxn];
 7 void dfs(int pos,LL lcm,int cnt) {
 8     if(pos == n) {
 9         if(cnt) {
10             LL num = m/lcm,tmp = (((num - 1)*num)>>1)*lcm;
11             ret += (cnt&1)?tmp:-tmp;
12         }
13         return;
14     }
15     if(lcm%a[pos] == 0) return;
16     dfs(pos + 1,lcm,cnt);
17     LL LCM = lcm*a[pos]/__gcd(lcm,a[pos]);
18     if(LCM < m) dfs(pos + 1,LCM,cnt + 1);
19 }
20 int main() {
21     int kase,cs = 1;
22     scanf("%d",&kase);
23     while(kase--) {
24         scanf("%d%d",&n,&m);
25         for(int i = 0; i < n; ++i) {
26             scanf("%I64d",a + i);
27             a[i] = __gcd(a[i],static_cast<LL>(m));
28         }
29         sort(a, a + n);
30         n = unique(a,a + n) - a;
31         ret = 0;
32         if(a[0] == 1) ret = (static_cast<LL>(m)*(m-1))>>1;
33         else dfs(0,1,0);
34         printf("Case #%d: %I64d\n",cs++,ret);
35     }
36     return 0;
37 }

时间: 2024-12-10 18:40:57

HDU 5514 Frogs的相关文章

Hdu 5514 Frogs (容斥原理)

题目链接: Hdu 5514 Frogs 题目描述: 有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少? 解题思路: 第一反应就是容斥,也是没谁了.但是重现赛的时候并没有做出来,自己写了一个容斥然后挂掉了,今天看到大神的方法,感觉这种处理方法有点神!(还是见的题目太少,太弱了) 第i只青蛙只能走到gcd(ai, m)的位置,我们就可以把m的因子提取出来,然后对青蛙能走到的因子位置打标记.青蛙能走到vis[i]因子位置就把vis[i]标记为

hdu 5514 Frogs(容斥)

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 a

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

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 a

hdu 5514 容斥原理

hdu 5514 题意: 有 n 只青蛙,一开始都在 0 点.有一堆围成一圈的石子,石子的编号是从 0 ~ (m-1). 所有青蛙只能顺时针跳,每个青蛙可以一次跳a[i]格.问这些青蛙踩过的石子的编号总和是多少? tags:  容斥经典题. 对 m 分解因子,对每个因子求贡献. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")

HDU 5514

题意: 给你 N 个数 和 一个 M: 对于 每一个 Ni , 乘以 K 取摸 M 都有一个 集合, 把所有集合合并, 求和 Σ ai ( ai → K * Ni % M ) 思路 : 最开始 直接求一边gcd , 然后容斥.... 结果状态有 2 ^  (1e4).... 反着求 M 的约数, 然后记录要用到的约数, 对于这些进行容斥就好了(不能状压) #include<bits/stdc++.h> using namespace std; typedef long long LL; con

hdu 5956 The Elder 2016ACM/ICPC沈阳赛区现场赛I

Problem Description Once upon a time, in the mystical continent, there is a frog kingdom, ruled by the oldest frog, the Elder. The kingdom consists of N cities, numbered from east to west. The 1-th city, which is located to the east of others, is the

HDU 4004 The Frog&#39;s Games(基本算法-贪心,搜索-二分)

The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The

hdu 4004 The Frog&#39;s Games【二分】

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3980    Accepted Submission(s): 1931 Problem Description The annual Games in frogs' kingdom started again. The most famous game

hdu 4004 The Frog&#39;s Games 【二分】

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4565    Accepted Submission(s): 2225 Problem Description The annual Games in frogs' kingdom started again. The most famous game