HDU 5514 Frogs(容斥原理)
2024-10-20 16:41:22
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5514
【题目大意】
m个石子围成一圈,标号为0~m-1,现在有n只青蛙,每只每次跳a[i]个石子,
问能被青蛙跳到的石子一共有几个
【题解】
我们发现k*gcd(m,a[i])的位置均可以被跳到,那么我们首先筛出m的约数,
判断其是否被覆盖到,不考虑重复的情况下,
每个被覆盖到的约数的贡献为x*((m-1)/x)*((m-1)/x+1)/2,
但是约数的倍数也为约数的情况被重复计算,因此我们按约数从大到小容斥计算答案。
【代码】
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
int T,n,m,p[20010],mark[20010],x,tot;
LL dp[20010];
int main(){
scanf("%d",&T);
for(int Cas=1;Cas<=T;Cas++){
memset(dp,0,sizeof(dp));
memset(mark,0,sizeof(mark));
scanf("%d%d",&n,&m); tot=0;
for(int i=1;i*i<=m;i++){
if(m%i==0){
p[++tot]=i;
if(i*i!=m)p[++tot]=m/i;
}
}sort(p+1,p+tot+1);
for(int i=1;i<=n;i++){
scanf("%d",&x);
int GCD=__gcd(x,m);
for(int j=1;j<=tot;j++)if(p[j]%GCD==0)mark[j]=1;
}LL ans=0;
for(int i=tot;i;i--)if(mark[i]){
int t=(m-1)/p[i];
dp[i]=1LL*t*(t+1)/2*p[i];
for(int j=i+1;j<=tot;j++)if(mark[j]&&p[j]%p[i]==0)dp[i]-=dp[j];
ans=ans+dp[i];
}printf("Case #%d: %lld\n",Cas,ans);
}return 0;
}
最新文章
- 【Win10】SplitView控件
- 咪咕视讯与美国AR公司ODG达成战略合作,联合打造尖端产品
- caffe学习系列(4):视觉层介绍
- win7,vs2010,asp.net项目中修改外部js文件,在调试时加载的还是旧文件
- 用PHP获取系统时间时,时间比当前时间少8个小时
- UVALive 6269 Digital Clock --枚举,模拟
- 记录下 QT Linux 静态编译遇到的坑
- 50道经典的JAVA编程题(46-50)
- VS快速定位文件、代码插件——DPack
- python文件批量改名
- Hadoop基准测试(转载)
- 升级到iis7 的web.config配置
- iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)
- CevaEclipse - 编译器attribute扩展
- An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON
- &#39;abc&#39; 转换成[a, b, c]一道面试题的思考
- Mui Webview下来刷新上拉加载实现
- TextBox使用技巧--转载
- spark Transformations算子
- jQuery事件学习
热门文章
- jq消除网页滚动条
- 2017 ACM暑期多校联合训练 - Team 3 1008 HDU 6063 RXD and math (莫比乌斯函数)
- Masquerade strikes back Gym - 101911D(补题) 数学
- webgote的例子(6)SQL注入(盲注)
- 73.Vivado使用误区与进阶——在Vivado中实现ECO功能
- linux 进程优先级 之设置实时进程 (另一种方式是设置nice值)【转】
- eclipse 常见问题之字体更改、添加注释模板
- php+mysql缓存技术的实现
- droupout
- NSBundle pathForResource is NULL 取不到值