HDU5514 Frogs
/*
HDU5514 Frogs
http://acm.hdu.edu.cn/showproblem.php?pid=5514
容斥原理
*
*
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
//#define test
using namespace std;
const long long Nmax=1e5;
long long n,m,a[Nmax];
long long book[Nmax];
long long p[Nmax];
int cnt;
long long num[Nmax];
long long gcd(long long a,long long b)
{
if(b==0LL)
return a;
return gcd(b,a%b);
} int main()
{
long long t;
#ifdef test
while()
{
long long a,b;
scanf("%lld%lld",&a,&b);
printf("%lld\n",gcd(a,b));
}
#endif
scanf("%lld",&t);
for(long long ttt=;ttt<=t;ttt++)
{
scanf("%lld%lld",&n,&m);
int flag=;
for(long long i=;i<=n;i++)
{
scanf("%lld",&a[i]);
a[i]=gcd(a[i],m);
if(a[i]==)
flag=;
}
if(flag)
{
long long ans=(m-1LL)*m/2LL;
printf("Case #%lld: ",ttt);
printf("%lld\n",ans);
continue;
}
long long ans=0LL;
cnt=;
for(int i=;i*i<=m;i++)
{
if(m%i)
continue;
p[++cnt]=i;
if(i*i!=m)
p[++cnt]=m/i;
}
sort(p+,p++cnt);
for(int i=;i<=cnt;i++)
book[i]=num[i]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=cnt;j++)
if(p[j]%a[i]==)
book[j]=;
}
for(int i=;i<=cnt;i++)
{
if(book[i]!=num[i])
{
long long tmp=m/p[i];
ans+=tmp*(tmp-1LL)/2LL*p[i]*(book[i]-num[i]);
tmp=book[i]-num[i];
for(int j=i+;j<=cnt;j++)
if(p[j]%p[i]==)
num[j]+=tmp;
}
} printf("Case #%lld: ",ttt);
printf("%lld\n",ans);
}
return ;
}
HDU5514 Frogs的更多相关文章
- 从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...
- 【做题】hdu5514 Frogs——另类容斥
题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...
- POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)
题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...
- CF# Educational Codeforces Round 3 F. Frogs and mosquitoes
F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
- Frogs' Neighborhood
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 7920 Accepted: 33 ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6076 Accepted: 26 ...
- codeforces 609F. Frogs and mosquitoes 二分+线段树
题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...
随机推荐
- Intent 使用方法全面总结
调用拨号程序 // 给移动客服10086拨打电话 Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Inte ...
- Mac上搭建android环境:Android Studio+GreenVPN
1.下载Android Studio,https://developer.android.com/sdk/index.html 2.使用GreenVPN,感觉还能够.18/月.http://www.g ...
- Android之——ContentProvider操作XML
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47682559 不多说,不废话,直接上代码,大家一看都懂得 /** * Conten ...
- CentOS6.8 安装 mysql 5.6
安装前的准备: 1.确认是否安装过mysql: yum list installed | grep mysql 2.删除系统自带的mysql及其依赖命令: yum -y remove mysql-li ...
- oc24--description
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; double ...
- Linux - 网络相关指令
系统时间与开关机 查看系统时间 date 查看硬件日期 hwclock 学习Linux不必全部指令都会,只要记住主要常用的几个就可以了.--MK 关机命令 shutdown init reboot p ...
- 2014/09/20 关于ArrayList的几种操作
1.删除ArrayList集合元素 删除ArrayList集合里面的元素时,提供了Clear方法,Remove方法,RmoveAt方法和RemoveRange方法. Clear方法是移除所有的元素 R ...
- 函数的arguments
1.arguments a.只能在函数内部被访问. b.arguments是一个伪数组(有下标索引,可以存放多个值,但是他里面没有数组的方法.) c.arguments里面存的是什么?函数的实际参数传 ...
- python3遍历选中文件夹下的文件【GUI编程】
功能介绍 如标题 使用截图 1.第一步:运行python程序 2.第二步:点击按钮选中文件夹 3.运行结束: 1. 显示选中文件夹路径 2. 遍历打印文件夹内文件 代码 import os #程序功能 ...
- deeplearning4j – 分布式DL开源项目
原文链接:http://www.52ml.net/16157.html Deeplearning4j is the first commercial-grade deep learning libra ...