HDU_5514_Frogs
Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2957 Accepted Submission(s): 946
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.
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).
2 12
9 10
3 60
22 33 66
9 96
81 40 48 32 64 16 96 42 72
Case #2: 1170
Case #3: 1872
- 观察对于n和m较小的时候的样例不难发现可以到达的点都是k*gcd(ai,m)<m
- 但是对于给出的n个a[i]我们不能保证在计算过程中对于同一个点只计算一次
- 考虑容斥原理解题
- gcd(ai,m)=di的结果都属于m的因数集合D(m)
- 如果 u*ai==v*aj 并且 u*ai 属于D(m),那么对于 u*ai的倍数就重复计算了一次,我们就应该在新的计算过程中减去多加的次数
- 对于多加的次数这个变量的维护可以通过记录当前因数被使用的次数来实现,1-被使用次数就是当前因数对应增量的倍数
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; LL gcd(LL x, LL y){
return y?gcd(y,x%y):x;
}
int T, tot;
LL n, m, a, d, k, ans;
LL b[maxn], c[maxn], e[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
for(int kase=;kase<=T;kase++){
ans=0LL;
tot=;
scanf("%lld %lld",&n,&m);
for(LL i=1LL;i<=(LL)sqrt(m);i++)
if(==m%i){
c[tot++]=i;
if(i!=m%i)
c[tot++]=m/i;
}
sort(c,c+tot);
memset(b,,sizeof(b));
memset(e,,sizeof(e));
for(int i=;i<=n;i++){
scanf("%lld",&a);
d=gcd(a,m);
b[lower_bound(c,c+tot,d)-c]=;
}
for(int i=;i<tot;i++)
if(b[i])
for(int j=i+;j<tot;j++)
if(c[j]%c[i]== && b[j]==)
b[j]=;
for(int i=;i<tot;i++)
if(b[i]){
LL cnt=b[i]-e[i];
k=(m-)/c[i];
// k=(m/c[i])-1;
ans+=cnt*k*(c[i]+k*c[i])/;
for(int j=i+;j<tot;j++)
if(c[j]%c[i]==)
e[j]+=cnt;
}
printf("Case #%d: %lld\n",kase,ans);
}
}
return ;
}
HDU_5514_Frogs的更多相关文章
随机推荐
- [svc]expect的爱恨情仇
背景 openvpn生成证书想把它做成一键化,这样添加新用户时候就方便 遇到的问题 我的代码 gg_vpn_keys.exp #!/usr/bin/expect set user [lindex $a ...
- 每日英语:Investing the Downward Dog Way? Adviser Suggests Deep Breaths
When the Dow Jones Industrial Average hit a new record this past March, Brent Kessel awoke at 3:30 a ...
- 开源搜索引擎评估:lucene sphinx elasticsearch (zhuan)
http://lutaf.com/158.htm ************************ 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch s ...
- MySQL 使用 SSL 连接(附 Docker 例子)
查看是否支持 SSL 首先在 MySQL 上执行如下命令, 查询是否 MySQL 支持 SSL: mysql> SHOW VARIABLES LIKE 'have_ssl'; +-------- ...
- C# 注册表修改 立即生效 [转]
修改注册表后不重启计算机边生效. const int WM_SETTINGCHANGE = 0x001A; const int HWND_BROADCAST = 0xffff; IntPtr resu ...
- scp采用无密码在两台linux服务器之间传输数据
一.root用户: 1. 在主机A上执行如下命令来生成配对密钥: ssh-keygen -t rsa 按照提示操作,注意,不要输入passphrase.提示信息如下 Generating public ...
- 应用DataAdapter对象填充DataSet数据集
private void Form1_Load(object sender, EventArgs e) { string strCon = "Server=localhost;User Id ...
- java调用kettle_导入jar包(1)
版权声明:本文为博主原创文章,未经博主允许不得转载. Java调用Kettle执行任务或转换,需要使用Kettle中的jar,可以先导入lib目录中的几个基本的jar,如:kettle-core.ja ...
- sql 追踪 神器
http://www.thinkphp.cn/download/690.html 一个中国人开发的php工具箱此工具能几秒钟追踪出sql 数据库操作, 能分析出 Thinkphp3.2 的任意sql ...
- jQuery 中get 和post 方法传值注意事项
用 jQuery 的都知道,jQuery 的 get 和 post 方法有三个参数:地址,数据 和回调函数,但我们知道地址也可以跟随数据的(形如:get_data.php?v1=1&v2=2) ...