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博客阅读原文】。感谢您的支持!

您可能感兴趣的

刚刚,ACM公布了2017年图灵奖得主:荣誉归于体系架构... 刚刚,美国计算机协会(ACM)宣布 John L. Hennessy 和 David A. Patterson 荣获 2017 年图灵奖。目前这两位学者都供职于...
2015 ACM/ICPC 北京赛区网络赛解题报告汇总... 欢迎转载,转载请注明出处。 针对2015 ACM/ICPC 北京赛区网络赛(9月20日),本日志汇总网上ACMer们发布的解题报告,方便大家...
2013 ACM/ICPC 南京网络赛解题报告汇总 欢迎转载,转载请注明出处。 针对2013 ACM/ICPC 南京网络赛(9月21日),本日志汇总网上ACMer们发布的解题报...
2015 ACM/ICPC 上海赛区网络赛解题报告汇总... 欢迎转载,转载请注明出处。 针对2015 ACM/ICPC 上海赛区网络赛(9月26日),本日志汇总网上ACMer们发布的解题报告...
使用阿里云ACM简化你的Spring Cloud微服务环境配置管理... 本文我们就如何使用阿里云ACM这样的配置管理产品在Spring Cloud中替代Spring Cloud Config帮助简化环境配置管理做一个简单的示例...