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

您可能感兴趣的

2013 ACM/ICPC 长春网络赛解题报告汇总 欢迎转载,转载请注明出处。 针对2013 ACM/ICPC 长春网络赛(9月28日),本日志汇总网上ACMer们发布的解题报告,方便大家互相学习。欢迎补充和完善本日志。 感谢所有及时发布解题报告,和大家分享知识的各位神牛。 ...
袁泉创办国内首家决策智能公司,ACM全球总决赛发布星际争霸II赛题(附视频)... 去年乌镇围棋峰会上,AlphaGo作为队友加入古力对阵连笑之后,国内就很少再有人机协作的新闻了。但人机协作能力的提升,对人类和AI的相互学习、互相借力,使得人类自身潜能不断释放都有着重要意义。柯洁虽败于AlphaGo但棋力大涨就是最好的说明。 本周,国内领先的智能决策公司启元世界在ACM挑战赛...
Jupyter receives the ACM Software System Award It is our pleasure to announce that Project Jupyter has been awarded the 2017 ACM Software System Award , a significant honor for the project. We ...
2017 ACM/ICPC Asia Regional Qingdao Online HDU 621... 传送门: 点击打开链接 Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) T...
2014 ACM/ICPC 鞍山赛区现场赛解题报告汇总... 欢迎转载,转载请注明出处。 针对2014 ACM/ICPC 鞍山赛区现场赛(10月19日),本日志汇总网上ACMer们发布的解题报告,方便大家互相学习。欢迎补充和完善本日志。感谢所有及时发布解题报告,和大家分享知识的各位神牛。 A . Twelve Months ...