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. [svc]salt安装lamp

    在批量安装软件前,先找台测试机yum装一遍,看是否报错等,是否依赖包全等 . 本次我们在dev环境下搞. 先看一下已搞成功的目录结构 定义dev环境的第二个好处 搞清楚逻辑结构和调用关系很重要,不然之 ...

  2. Visual Studio:error MSB8020

    状况如下: error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build ...

  3. swift 类型.

    swift 类型 变量声明 用let来声明常量,用var来声明变量 可以在一行中声明多个常量或者多个变量,用逗号隔开 var x = 0.0, y = 0.0, z = 0.0 类型安全 Swift ...

  4. 上传图片流到服务器(AFN方法) (多张图片)(图片流)

      上传图片流到服务器(AFN方法) (多张图片)(图片流) 第一步//获取图片 UIAlertController *actionSheet = [UIAlertController alertCo ...

  5. python爬虫解析库之re模块

    re模块 一:什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中, ...

  6. [I2C]pca9555应用层测试代码

    注意点: 如果在设置I2C_SLAVE的时候,提示device_busy,可以使用I2C_SLAVE_FORCE, 在驱动里面二者对应同一个case语句 应用层可以调用接口:i2c_smbus_wri ...

  7. 虚拟化–操作系统级 LXC Linux Containers内核轻量级虚拟化技术

    友情提示:非原文链接可能会影响您的阅读体验,欢迎查看原文.(http://blog.geekcome.com) 原文地址:http://blog.geekcome.com/archives/288 软 ...

  8. 关于Unity的C#基础学习(五)

    一.get/set访问器 class Person{ int my_age; //默认私有权限 int sex; //属性,类似于函数,但是又不是函数的东西 public int age{ get{ ...

  9. The Properties of Posterior of Topic Model

    1.Tang, Jian, et al. "Understanding the Limiting Factors of Topic Modeling via Posterior Contra ...

  10. 第二百六十八节,Tornado框架-路由映射之二级域名支持,html模板继承以及导入

    Tornado框架-路由映射之二级域名支持,html模板继承以及导入 二级域名路由映射add_handlers()设置二级域名路由映射 注意:二级域名需要结合服务器ip绑定域名 框架引擎 #!/usr ...