2015ACM/ICPC亚洲区沈阳站-重现赛 HUD 5514 Frogs (容斥原理+GCD)

综合技术 2017-10-02 阅读原文

Frogs




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




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








a


i








stones in a single step, which means from stone





j

m
o

m





to stone





(
j
+



a


i



)

m
o

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




10


4



,

1

m




10


9



)




.
The second line contains





n





integers








a


1



,



a


2



,

,



a


n







, where








a


i








denotes step length of the





i




-th frog





(
1




a


i







10


9



)




.
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 只青蛙, m 个石头; 第i个青蛙 每次跳 ai个石头 石头围城环 在 0-m-1 内循环

求 n只青蛙 不重复跳过的 石头标号总数;

【思路】

容斥原理+ GCD

示例 2 12 9,10

可以发现 9 —— 走的石头为 3 6 9 10—— 走的石头为 2 4 6 8 10 重复点为6 容斥原理

走的步数为 2 3 4 6 8 9 10 可以发现 是 的倍数GCD(ai,m) 即 2 和3的倍数

m的因子为 1 2 3 4 6 8 12

是GCD(ai,m) 倍数的是

2 3 4 6

2 4 6 8 10 (5个)

3 6 9 ( 3个)

4 8 (2 个)

6 (1 个)

容斥原理 应用 : 用 vis 标记 2 3 4 6 这样的数 标记为 1

用 num 来记录 2 3 4 6 中 存在倍数的数

num【】一次为 0 0 1 2 然后 ans += 前x 项和 * (vis【i】— num【i】) (非常巧妙)

【代码实现】

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%dn",n)
#define lson rt << 1, l, mid
#define rson rt <>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
ll inv2(ll b){return qpow(b,MOD-2);}

ll n,m;
ll a[maxn];
ll vis[maxn];
ll qoop[maxn];
ll num[maxn];
ll cot;
void init()
{
    cot=0;
    for(int i=1;i>t;
    int cont=0;
    while(t--)
    {
        scanf("%lld %lld",&n,&m);
        mem(a,0);
        mem(num,0);
        mem(vis,0);
        mem(qoop,0);
        init();
        sort(qoop+1,qoop+cot+1);
        for(int i=1;i<=n;i++) 2="" {="" ll="" x;="" scanf("%lld",&x);="" temp="gcd(x,m);" for(int="" j="1;j<=cot;j++)" if(qoop[j]%temp="=0)" vis[j]="1;" }="" ans="0;" i="1;i<=cot;i++)" if(vis[i]!="num[i])" tm="(m-1)/" qoop[i];="" ans+="tm" *="" (tm+1)="" *qoop[i]="" (vis[i]-num[i]);="" if(qoop[j]%qoop[i]="=0)" num[j]+="vis[i]-num[i];" printf("case="" #%d:="" %lldn",++cont,ans);="" return="" 0;="" }
  

123

CSDN博客

责编内容by:CSDN博客阅读原文】。感谢您的支持!

您可能感兴趣的

Hortonworks at ACM SIGMOD/PODS 2018 The annual ACM SIGMOD/PODS Conference is a leading international forum for database researchers, practitioners,...
AI大时代,姚班“大神”和ACM班“天才”的一场巅峰对话... 中国计算机界的“黄埔军校”非清华的姚班、上海交大的ACM班莫属,这两个班脱胎于两所大学的计算机教育改革,将一群已经在计算机领域里展现出惊人天赋的少年天才们汇聚在一起,向着更深、更远的技术边界探索。如今,这一众少年天才中,有很多人已经成为 ...
ACM国际大学生程序设计竞赛成为快手等企业HR的战场... 中新网4月20日电 19日,第42届ACM国际大学生程序设计竞赛(简称ACM-ICPC)全球总决赛落在北京大学落下帷幕。这是被誉为“计算机编程领域的奥林匹克竞赛”的 ACM-ICPC全球总决赛第三次来到中国,由北京大学和中国科协青少年科技中...
(关系幂运算->n长度的最短路)河南省第十届ACM省赛题目:问题 I: Transmit in... 问题 I: Transmit information 时间限制: 3 Sec 内存限制: 128 MB 提交: 2 解决: 2 ...
2013 ACM/ICPC 长沙赛区现场赛解题报告汇总... 2013 ACM/ICPC 长沙赛区现场赛解题报告汇总 十一月 17th, 2013 | Posted by huangkun ...