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 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
题意:有n只青蛙,m个石头(围成圆圈)。第i只青蛙每次只能条ai个石头,问最后所有青蛙跳过的石头的下标总和是多少?
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=2*1e5+10;
int factor[N],num[N],appear[N],step[N];
ll add[N]; int gcd(int a,int b)
{
if(b==0) return a;
else return gcd(b,a%b);
} int main()
{
int cas,n,m,kk=0;
SC("%d",&cas);
while(cas--){
SC("%d%d",&n,&m);
int cnt=0;MM(num,0);MM(appear,0);
for(int i=1;i<=n;i++) SC("%d",&step[i]);
for(int i=1;i*i<=m;i++) if(m%i==0){
factor[++cnt]=i;
if(i*i!=m) factor[++cnt]=m/i;
}
sort(factor+1,factor+cnt+1);
cnt--;
for(int i=1;i<=cnt;i++){
ll k=(m-1)/factor[i];
add[i]=k*(k+1)/2*factor[i];
} for(int i=1;i<=n;i++){
int k=gcd(step[i],m);
for(int j=1;j<=cnt;j++)
if(factor[j]%k==0) appear[j]=1;
} ll ans=0;
for(int i=1;i<=cnt;i++) if(num[i]!=appear[i]){
ans+=add[i]*(appear[i]-num[i]);
for(int j=i+1;j<=cnt;j++)
if(factor[j]%factor[i]==0) num[j]+=(appear[i]-num[i]);
}
printf("Case #%d: %lld\n",++kk,ans);
}
return 0;
}

  分析:

1.一个数的因子个数大概是log级别;

2.ax+by=c有非负整数解的条件是c%gcd(a,b);取余

=>ax+by=k*gcd(a,b) =>ax%b=k*gcd(a,b)%b =>ai*x%m=k*gcd(ai,m)%m;

所以,青蛙能走到的格子数是k*gcd(ai,m),而gcd(ai,m)又必然是m的因数,所以可以先分解出m的因子

3.但是因为有些格子可能同时被多只青蛙走,因此需要容斥一下,设appear[i]为格子i应该走的次数(只有0,1)两个值,num[i]为格子i到当前为止实际走的次数,如果当前appear[i]>appear[i],就需要加,否则减去,然后将是当前因子factor[i]倍数的因子也同时跟新num值

hdu 5514 Frogs 容斥思想+gcd 银牌题的更多相关文章

  1. hdu 5514 Frogs(容斥)

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  2. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  3. ACM-ICPC 2015 沈阳赛区现场赛 F. Frogs && HDU 5514(容斥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意:有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过xi个石子.问所 ...

  4. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  5. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

  6. 很好的容斥思想 HDU 5514

    题目描述:有n只青蛙,m个石头(围成圆圈).第i只青蛙每次只能条a[i]个石头,问最后所有青蛙跳过的石头的下标总和是多少? 思路:经过绘图我们发现,每次跳过的位置一定是k*gcd(a[i], m).然 ...

  7. HDU 5514 Frogs (容斥原理)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5514 题意 : 有m个石子围成一圈, 有n只青蛙从跳石子, 都从0号石子开始, 每只能越过a[i] ...

  8. HDU 5514 Frogs(容斥原理)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5514 [题目大意] m个石子围成一圈,标号为0~m-1,现在有n只青蛙,每只每次跳a[i]个石子, ...

  9. HDU 5514 Frogs

    Frogs Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5514 ...

随机推荐

  1. Closest Common Ancestors (Lca,tarjan)

    午时刷题,难甚,遂小憩于桌上,惊醒,于梦中有所得,虽大声曰:吾已得tarjan之奥秘! 关于tarjan算法,其实就是一个递归加并查集的应用. 大致代码: #include<bits/stdc+ ...

  2. PHP无限极菜单

    权限表结构 CREATE TABLE `blog_auth` ( `id` ) unsigned NOT NULL AUTO_INCREMENT COMMENT '序号', `pid` ) NOT N ...

  3. Python爬虫—requests库get和post方法使用

    目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...

  4. gitlab LFS 的应用实践

    今天看到的gitlab LFS的文档,将自己的理解整理成博客,加深自己的印象.具体gitlab LFS的介绍可以直接百度了,不在这里详细阐述.只提一下他的作用:LFS就是Large File Stor ...

  5. DVWA漏洞演练平台 - 文件上传

    DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法的环境,帮助w ...

  6. 使用 SQL的 for xml path来进行字符串拼接

    本篇主要讲怎么利用SQL的FOR XML PATH 参数来进行字符串拼接,FOR XML PATH的用法很简单,它会以xml文件的形式来返回数据. 我的讲解步骤: 1:构造初始数据 2:提出问题 3: ...

  7. YoloV3 训练崩溃

    经过排查  发现是这里出了问题 然后发现是标注文件里有 x=0 y=0  这样的数据,46_Jockey_Jockey_46_576.txt ,  那么肯定是标注文件出了问题!! 删除该标注文件即可. ...

  8. js之数据类型(对象类型——构造器对象——正则)

    正则(regular expression)描述了一种字符串的匹配式.一般应用在一些方法中,用一些特殊的符号去代表一些特定的内容,对字符串中的信息实现查找,替换,和提取的操作.js中的正则表达式用Re ...

  9. shell script 编程入门

    参考 <linux shell scripting cookbook> 控制台输出 结构化输出 #!/bin/bash #Filename: printf.sh printf " ...

  10. 忘记root密码,修改方法

    Linux的root密码修改不像Windows的密码修改找回,Windows的登录密码忘记需要介入工具进行解决.CentOS6和CentOS7的密码方法也是不一样的,具体如下: 首先是CentOS 6 ...