/**
题目:hdu6053 TrickGCD
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6053
题意:You are given an array A , and Zhu wants to know there are how many different array B satisfy the following conditions? * 1≤Bi≤Ai
* For each pair( l , r ) (1≤l≤r≤n) , gcd(bl,bl+1...br)≥2 思路:枚举2<=gcd<=misA; misA表示A数组最小的数。 当gcd==2. 贡献为: (a1/2)*(a2/2)*...*(an/2);所有2的倍数的组合。
当gcd==3. 贡献为: (a1/3)*(a2/3)*...*(an/3);所有3的倍数的组合。
当gcd==4.不需要计算因为在2中算过了。


gcd==6. 减去。因为2,3都算过6,所以多算了一次。
也就是按照容斥原理的做法。
如果gcd可以被一个素数的平方整除,那么该gcd不用计算。
否则:f(i) = (-1)^(k+1); k表示i这个数的素因子个数。 由于mu[i] = (-1)^k; 所以求mu之后取反。 对于确定的gcd==2,计算贡献:因为ai/2很多结果相同。[gcd,2*gcd)范围内的数/gcd的结果都是1,[2*gcd,3*gcd)范围内的数/gcd的结果都是2.。。。
所以贡献等于1^num1 * 2^num2 * 3^num3 ... (numi表示结果为1的数量。可以利用前缀和统计范围内的数。)
其他类比。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = 1e5 + ;
vector<P> gd;///value, num;
int sum[maxn];
int vis[maxn], mu[maxn];
int prime[maxn], cnt;
void init()///莫比乌斯
{
memset(vis,,sizeof(vis));
mu[] = ;
cnt = ;
for(int i=; i<maxn; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
mu[i] = -;
}
for(int j=; j<cnt&&i*prime[j]<maxn; j++)
{
vis[i*prime[j]] = ;
if(i%prime[j]) mu[i*prime[j]] = -mu[i];
else
{
mu[i*prime[j]] = ;
break;
}
}
}
for(int i = ; i < maxn; i++){
if(mu[i]!=){
gd.push_back(P(i,-mu[i]));
}
}
}
LL Pow(LL x, int y)
{
LL p = ;
while (y)
{
if (y & ) p = p*x%mod;
x = x*x%mod;
y >>= ;
}
return p;
}
int main()
{
int T, cas = ;
int n;
init();
cin >> T;
while (T--)
{
scanf("%d", &n);
ms(sum, );
int x, misx = INF;
for (int i = ; i < n; i++) {
scanf("%d", &x);
misx = min(misx, x);
sum[x]++;
}
for (int i = ; i < maxn; i++) {
sum[i] += sum[i - ];
}
LL ans = ;
int len = gd.size();
for (int i = ; i < len&&gd[i].first <= misx; i++) {
int gcd = gd[i].first;
LL cnt = ;
for (int j = gcd; j < maxn; j += gcd) {
cnt = cnt*Pow(j / gcd, sum[min(maxn - , j + gcd - )] - sum[j - ]) % mod;
}
ans = (ans + cnt*gd[i].second + mod) % mod;
//cout<<"gcd = "<<gcd<<endl;
//cout<<"ans = "<<ans<<endl;
}
//cout<<"ans = "<<ans<<endl;
printf("Case #%d: %lld\n", cas++, ans);
} return ;
}

hdu6053 TrickGCD 容斥原理的更多相关文章

  1. HDU-6053 TrickGCD

    题目连接: https://vjudge.net/problem/HDU-6053 Description You are given an array A , and Zhu wants to kn ...

  2. [Hdu-6053] TrickGCD[容斥,前缀和]

    Online Judge:Hdu6053 Label:容斥,前缀和 题面: 题目描述 给你一个长度为\(N\)的序列A,现在让你构造一个长度同样为\(N\)的序列B,并满足如下条件,问有多少种方案数? ...

  3. hdu 6053: TrickGCD (2017 多校第二场 1009) 【莫比乌斯 容斥原理】

    题目链接 定义f[n]表示n是最大公约数情况下的计数,F[n]为n是公约数情况下的计数 (可以和 http://www.cnblogs.com/Just--Do--It/p/7197788.html  ...

  4. HDU 6053 - TrickGCD | 2017 Multi-University Training Contest 2

    /* HDU 6053 - TrickGCD [ 莫比乌斯函数,筛法分块 ] | 2017 Multi-University Training Contest 2 题意: 给出数列 A[N],问满足: ...

  5. HDU 6053 TrickGCD —— 2017 Multi-University Training 2

    TrickGCD Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  7. hdu2848 Visible Trees (容斥原理)

    题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...

  8. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  9. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3028  Solved: 1460[Submit][Sta ...

随机推荐

  1. Java笔记19:Java匿名内部类

    匿名内部类也就是没有名字的内部类.正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写.但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 例1:不使用匿名内部类来实现抽象方 ...

  2. phpstorm快速跳转到错误行

    phpstorm的快捷键,在网上可以搜索出很多,唯有“快速跳到错误行”的快捷键几乎搜索不出来. 看着错误提示,要一行一行的用眼睛去看,心累. 贴在这里,随时取用,再也不怕忘记了. Shift + F2 ...

  3. 转: git的图文使用教程(巨详细)

    转自: http://blog.jobbole.com/78960/ Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集 ...

  4. TestNG 五 运行TestNG

    一.并行运行于超时 可以通过在suite标签中使用 parallel 属性来让测试方法运行在不同的线程中.这个属性可以带有如下这样的值: <suite name="My suite&q ...

  5. C++ new的nothrow关键字和new_handler用法

    C++ new的nothrow关键字和new_handler用法 new && new(std::nothrow) new(std::nothrow) 顾名思义,即不抛出异常,当new ...

  6. nginx+lua+redis 处理APK包替换

    nginx + lua +redis 安装与使用入门: http://huoding.com/2012/08/31/156 nginx httpEchoModule : http://wiki.ngi ...

  7. sqlserver远程备份到其他服务器

    直接将数据库备份到其他机器上 --如果xp_cmdshell没有启用,请先启用 sp_configure reconfigure go sp_configure reconfigure go --1. ...

  8. struts2中文件上传

    注意点 private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的 private String imageFileName;// ...

  9. Spark-Dependency

    1.Spark中採用依赖关系(Dependency)表示rdd之间的生成关系.Spark可利用Dependency计算出失效的RDD.在每一个RDD中都存在一个依赖关系的列表 private var ...

  10. 【转帖】Servlet 3.0 新特性详解

    http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 ...