频道栏目
首页 > 考试 > 其他 > 正文
HDU 5514 Frogs(容斥原理)——2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
2017-02-11 09:55:00         来源: 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 个青蛙跳过的个石头的编号的总和是多少。

解题思路:

一眼容斥(这个应该是很好想的),但是这个容斥的过程还是得好好想想的,首先我们通过观察规律,发现第 i 个青蛙能够跳的石头编号一定是 GCD(ai,m) , 所以第 i 个青蛙跳总的石头编号一定是 GCD(ai,m) 的倍数,而且是不超过 m?1 的,那么现在我们又得到了一个信息是 GCD 的信息,因为是 GCD 所以,这个 GCD 一定是 m 的因子数,那么我们首先将 m 的因子预先处理出来(注意是 因子),那么我们现在考虑每个因子 x 对答案做出的贡献: x?mx?(mx?1)2, 因为我们将因子 x 提出来以后就会变成一个 等差数列 ,然后首项加末项就等于 mx 然后一共有 mx?1 项,因为这样肯定会有多加上的(重复的),所以我们就要进行一个容斥的过程,首先我们将每个因子是 GCD(ai,m) 的数进行标记,将其标记为 1,然后在开一个数组 num[i] 表示的是第 i 个因子加了几次,然后剩下的就是加加减减了。

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
        
   
点击复制链接 与好友分享!回本站首页
上一篇:【Leetcode】409. Longest Palindrome
下一篇:【Leetcode】408. Valid Word Abbreviation
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站