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 ...
随机推荐
- #leetcode#Anagrames
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- PL SQL Developer client 连接server
安装完Oracle,PLSQL之后,在server中打开监听. 计算机右键-管理-服务和应用程序-服务-打开以Oracle开头的服务,特别是监听,这个最重要.详细如图所看到的. (1)配置监听的位置 ...
- Jungle Roads --hdoj
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 1-1restful简介及资源介绍
- js重定向
在现行的网站应用中URL重定向的应用有很多: 404页面处理.网址改变(t.sina转到weibo.com).多个网站地址(如:http://www.google.com/ .www.g.cn )等: ...
- js设计模式-命令模式
命令模式是一种组织型模式,主要用在把调用对象(用户界面.API和代理等)与实现操作的对象隔离开.也就是说 ,凡是两个对象间的互动方式需要更高的模块化程度时都可以用到这种模式. 命令模式的好处:1.提高 ...
- Blender插件之Panel
目标 [x] 总结Blender之Panel 总结 Blender之Panel需要从Blender界面组成开始理解. 直观上Blender的界面层次为 Editors ‣ Regions ‣ (Tab ...
- ROW_NUMBER() OVER()函数用法;(分组,排序),partition by (转)
1.row_number() over()排序功能: (1) row_number() over()分组排序功能: 在使用 row_number() over()函数时候,over()里头的分组以及排 ...
- 第十课: - 读取/写入Excel/Json格式数据
第 10 课 从DataFrame到Excel 从Excel到DataFrame 从DataFrame到JSON 从JSON到DataFrame In [1]: import pandas as pd ...
- 图的连通性问题的小结 (双连通、2-SAT)
图的连通性问题包括: 1.强连通分量. 2.最小点基和最小权点基. 3.双连通. 4.全局最小割. 5.2-SAT 一.强连通分量 强连通分量很少单独出题,一般都是把求强连通分量作为缩点工具. 有三种 ...