Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2957    Accepted Submission(s): 946

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较小的时候的样例不难发现可以到达的点都是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的更多相关文章

随机推荐

  1. 再谈API GateWay服务网关

    前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...

  2. iptables安装失败后-------------firewalld回归

    yum install firewalld systemctl stop iptables; systemctl mask iptables; systemctl unmask firewalld s ...

  3. lucene4.7学习总结 (zhuan)

    http://blog.csdn.NET/mdcmy/article/details/38167955?utm_source=tuicool&utm_medium=referral ***** ...

  4. 解决The markup in the document following the root element must be well-formed.

    出现问题的代码: <security-constraint> <web-resource-collection> <web-resource-name>Regist ...

  5. ADO.NET数据库应用开发_ExtendedProperties属性

    7.5.5 ExtendedProperties属性 ExtendedProperties属性用来获取存储自定义属性的集合.可以在该属性中增加附加的存储信息.它的扩展属性必须是字符串类型.当以XML的 ...

  6. UCOS2系统内核讲述(五)_初始化TCB详情

    Ⅰ.写在前面 学习本文之前可以参看我前面的文章: UCOS2系统内核讲述(四)_创建任务 上一篇文章讲述了关于函数“OSTaskCreateExt”创建任务函数体里面重要一些的内容,本文接着上一张讲述 ...

  7. Android基础总结(三)SQLite,ListView,对话框

    测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...

  8. 详解 Go 语言中的 time.Duration 类型

    swardsman详解 Go 语言中的 time.Duration 类型swardsman · 2018-03-17 23:10:54 · 5448 次点击 · 预计阅读时间 5 分钟 · 31分钟之 ...

  9. Linux上的free命令简介

    每次使用free时都比较迷惑,对于上面的内容一直都不是很清楚,今天仔细查了以下,和大家一起分享以下: 先看一下free的运行结果: free打印出的内存信息主要分为两种,一种是安装的内存,一种是用磁盘 ...

  10. 用Zend OPCache提高PHP的性能

    Zend OPCache的前身是Zend Optimizer + (Zend O+),在PHP5.5的发行版本中自带了Zend O+,并重新命名为:Zend OPCache.但是默认是没有启用的,可以 ...