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的更多相关文章
随机推荐
- nginx rewrite目录对换
/123/xxx----->xxx?id=123 [root@web01 default]# pwd /app/www/default [root@web01 └── sss └── index ...
- python-wechatAutoReword
python-微信自动回复功能,基于itchat 2017.9.6 实现群@自动回复 #! /usr/bin/env python3.5.2 # coding="utf-8" im ...
- spring cloud outh2
使用Spring Cloud Security OAuth2搭建授权服务http://www.blogjava.net/paulwong/archive/2016/09/16/431797.html? ...
- [ADC]Linux ADC驱动
ADC TI adc user guide: http://processors.wiki.ti.com/index.php/Linux_Core_ADC_Users_Guide 问题: 在tools ...
- 【2015/7/22】SqlServer卸载重装全攻略!
请大家大声地告诉我,哪个软件最恶心. 装了之后跟在电脑里面糊了一层泥,甩都甩不干净.之前手贱,重装系统后装了sqlserver2014的试用版.可惜过了半年试用期就到了.然后重装2012.2014卸载 ...
- 简单好用的包管理器 brew
Homebrew 是什么? macOS 上的包管理器,相当于 Debian 系的 apt-get ,或者是 Redhat 系的 yum . Homebrew 有什么用? 帮你安装一些系统默认没有安装但 ...
- CLion 2017 注册码
注册码使用时间2017-2018 CNEKJPQZEX-eyJsaWNlbnNlSWQiOiJDTkVLSlBRWkVYIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNz ...
- (转)内核container_of(ptr,type,member) 解析
container_of(ptr,type,member) 用于在已知结构体里面成员member和该成员指针ptr(就是地址)和结构体类型type, 返回该成员所在的结构体的指针(就是地址), 例如 ...
- Oracle Data Provider for .NET的使用(三)-ORACLE与.NET类型对应关系
想来这个是最重要的事情了,因为多数情况下,我们使用dbhelper来调用数据库的时候,是因为如下三个地方导致错误: 1.错误的sql语句:末尾多了分号,少了部分关键字 2.sql中的参数与parame ...
- css 五角星 (转)
1.前言 之前做的好几个项目中,都会遇到打分,评分,点赞这样的需求,写了很多次,每次需要再写的时候,就会翻出之前写过的代码,然后copy过来.总觉得这样的话没有进步,没有把知识放进脑袋里,所以,自己花 ...