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 ...
随机推荐
- C++设计模式之状态模式(二)
2.智能空调的设计与实现 某软件公司将开发一套智能空调系统: 系统检測到温度处于20---30度之间,则切换到常温状态:温度处于30---45度,则切换到制冷状态: 温度小于20度,则切换到制热状态. ...
- C语言高速入门系列(一)
C语言高速入门系列(一) 本系列引言: 本教程的宗旨是将C语言入门的内容进行关键知识点的提纯,将一些笼统的废话去除; 再进行压缩,然后将本章的关键知识点做成路线图的,能够更加方便地掌握学习的方向; ...
- h5-7 canvas
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【BZOJ3218】【UOJ#77】a + b Problem
题目 题目在这里 思路&做法 明显的最小割(其实是之前做过一道类似的题) S向每一个格子连容量为\(b_i\)的边 每一个格子向T连容量为\(w_i\)的边 对于格子\(i\)向满足条件的格子 ...
- ubuntu刚安装好之后apt-get使用异常
gaozhang 刚安装好之后,想执行apt-get update 任务,出现以下错误提示 提示说明apt正在执行,我们就野蛮的将apt进程杀.死即可,不过有点多,一个个kill 执行完之后再 ...
- Python 生成requirement 使用requirements.txt
第一步:python项目中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号.以便新环境部署. requirements.txt可以通过pip命令自动生成和安装 ...
- Analyzing resource wait related to memory/IO bottleneck
Analyzing resource wait related to memory bottleneck (RESOURCE_SEMAPHORE, PAGEIOLATCH_XX) sys.dm_os_ ...
- 利用JavaScript的%做隔行换色
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- vue-cli 结构
. |-- build // 项目构建(webpack)相关代码 | |-- build.js // ...
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'
ALTER TABLE table1 ADD COLUMN column1 VARCHAR(400) DEFAULT NULL; ERROR 1062 (23000): Duplicate entry ...