hdu5514 Frogs(容斥)
来源:互联网 发布:网上荣誉室源码 编辑:程序博客网 时间:2024/04/30 12:21
Frogs
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1463 Accepted Submission(s): 488
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
题目大意:
有
思路:
枚举约数+容斥原理
代码如下:
#include<iostream>#include<cmath>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<bitset>#include<map>typedef long long LL;using namespace std;const int maxn=10005;LL fac[maxn],gc[maxn],num[maxn];map<LL,bool>vis;LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b);}LL getfac(LL n){ int cnt=0; for(int i=1;i*i<=n;i++) { if(n%i==0) { fac[cnt++]=i; if(i*i!=n)fac[cnt++]=n/i; } } return cnt;//返回约数个数}LL getsum(int a1,int an,int cnt){ LL ans=(LL)(a1+an)*cnt/2; return ans;}int main(){ int T,n,l,x,cas=1; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&l); vis.clear(); memset(num,0,sizeof(num)); int cnt=getfac(l); int index=0; for(int i=0;i<n;i++) { scanf("%d",&x); int gcdxl=gcd(x,l); if(vis[gcdxl])continue; else vis[gcdxl]=true,gc[index++]=gcdxl; } sort(fac,fac+cnt); LL ans=0; for(int i=0;i<index;i++)for(int j=0;j<cnt;j++)if(fac[j]%gc[i]==0)num[j]=1; for(int i=0;i<cnt;i++) { if(num[i]==0)continue; int step=(l-1)/fac[i]; LL k=getsum(fac[i],fac[i]+(step-1)*fac[i],step); ans+=num[i]*k;//k为一个等差数列的前n项和 for(int j=i+1;j<cnt;j++) { if(fac[j]%fac[i]==0) { num[j]-=num[i]; } } } printf("Case #%d: %lld\n",cas++,ans); }}
0 0
- hdu5514 Frogs(容斥)
- hdu5514 Frogs(容斥)
- HDU5514 Frogs 容斥原理
- HDU5514 Frogs (gcd + 容斥原理)
- Frogs (hdu5514)——2015ACM/ICPC亚洲区沈阳站(容斥定理)
- HDU5514 Frogs
- hdu5514--frogs
- hdu5514(有技巧的容斥)
- HDU5514(容斥)
- 欧拉函数(或者容斥)-HDU5514
- HDU 5514 Frogs (容斥定理)
- HDU 5514 Frogs(容斥)
- HDU 5514 Frogs(容斥)
- HDU 5514 (Frogs) (容斥原理)
- HDU 5514 Frogs(容斥问题)
- Frogs(容斥+数论)
- hdu5514容斥原理和群论
- hdu 5514 Frogs 容斥
- Linux_php-fpm自启动脚本(CentOS7中可用)
- 383. Ransom Note
- R中常用的数据分析函数
- PhotoView 图片缩放功能
- 快速排序以及找到第k小的元素
- hdu5514 Frogs(容斥)
- ncurses--读书笔记4
- ApplicationContext的初始化问题
- GitHub入门之一:使用github下载项目
- 剑指Offer——回溯算法解迷宫问题(java版)
- CString与string、char*的区别和转换
- UVa OJ 1442 - Cav
- 哪些CSS属性可以继承
- 我的心得体会