Frogs
来源:互联网 发布:中超数据库 2017 门将 编辑:程序博客网 时间:2024/05/20 05:05
Problem Description
There are m stones lying on a circle, and n frogs are jumping over them.
The stones are numbered from0 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 stone0 , 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.
The stones are numbered from
All frogs start their jump at stone
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 integern and m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109) .
The second line containsn integers a1,a2,⋯,an , where ai denotes step length of the i -th frog (1≤ai≤109) .
meaning the total number of test cases.
For each test case, the first line contains two positive integer
The second line contains
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
32 129 103 6022 33 669 9681 40 48 32 64 16 96 42 72
Sample Output
Case #1: 42Case #2: 1170Case #3: 1872
思路: 设GCD(a,m)=k,则a青蛙跳过的石头一定是k的倍数;我们可以枚举m的因子;标记所有整除a的因子;然后再用容斥定理求解:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;long long q[20000],p[10000];long long GCD(long long x,long long y){ long long a,b; a=x>=y?x:y; b=x<=y?x:y; do { x=b; b=a%b; a=x; }while(b); return a;}int main(){ long long t,n,m,a,sum,cnt,d; scanf("%lld\n",&t); for(int o=1;o<=t;o++) { sum=0,cnt=0; scanf("%lld %lld",&n,&m); p[cnt++]=1; memset(q,0,sizeof(q)); for(int i=2;i*i<=m;i++) { if(m%i==0) { if(i*i==m) p[cnt++]=i; else { p[cnt++]=i; p[cnt++]=m/i; } } } sort(p,p+cnt); for(int i=1;i<=n;i++) { scanf("%lld",&a); a=GCD(a,m); for(int j=0;j<cnt;j++) if(p[j]%a==0) q[j]=1; } for(int i=0;i<cnt;i++) if(q[i]!=0) { long long h=(m-1)/p[i]; sum=sum+(h+1)*h/2*p[i]*q[i]; for(int j=i+1;j<cnt;j++) if(p[j]%p[i]==0) q[j]=q[j]-q[i]; } printf("Case #%d: %lld\n",o,sum); } return 0;}
阅读全文
0 0
- Frogs
- Frogs' Neighborhood
- Frogs HDU
- HDU5514 Frogs
- Frogs HDU
- hdu5514--frogs
- POJ 1659 Frogs' Neighborhood
- POJ_1659 Frogs' Neighborhood
- POJ 1659 Frogs' Neighberhood
- poj 1659 Frogs' Neighborhood
- POJ 1659 Frogs' Neighborhood
- POJ:1659Frogs' Neighborhood
- PKU 1659 Frogs' Neighborhood
- Poj 1659 Frogs' Neighborhood
- poj 1659 Frogs' Neighborhood
- poj1659 - Frogs' Neighborhood
- poj 1659 Frogs' Neighborhood
- Havel定理---Frogs' Neighborhood
- 昂贵的聘礼 POJ
- SQL的GROUP BY详解
- Untitled ( DFS 递归 )
- Wavelet(续)
- 关于jmeter 断言结果(成功或失败表现)
- Frogs
- 微信小程序实例--知乎日报小程序
- Linux查看和剔除当前登录用户
- Struts中ActionContext.getContext()的用法
- PHP 解决中文输出成乱码
- Android 设置adjustResize失效
- 在Python中实现PageFactory模式
- 安装cocoapods时候出现错误如下: sudo gem install cocoapods ERROR: While executing gem … (OpenSSL::SSL::SSLErro
- 使用Kotlin配合RxJava网络请求