TrickGCD

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3401    Accepted Submission(s): 1268

Problem Description
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

 
Input
The first line is an integer T(1≤T≤10) describe the number of test cases.

Each test case begins with an integer number n describe the size of array A.

Then a line contains n numbers describe each element of A

You can assume that 1≤n,Ai≤105

 
Output
For the kth test case , first output "Case #k: " , then output an integer as answer in a single line . because the answer may be large , so you are only need to output answer mod 109+7
 
Sample Input
1
4
4 4 4 4
 
Sample Output
Case #1: 17
 
Source
 
思路:枚举gcd ,对于当前的i,分区间地计算出gcd为i倍数的数列总数,之后利用容斥来减掉重复的部分,要保证每次减掉都是确定的倍数因此要倒着减。
代码:
 #include<bits/stdc++.h>
#define db double
#define ll long long
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define fr(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int N=1e5+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const int inf = 0x3f3f3f3f;
ll a[N];
ll sum[N];
ll qpow(ll x,ll n)
{
ll ans=;x%=mod;
for(;n>;n>>=){if(n&) ans=(ans*x)%mod;x=x*x%mod;}
return ans;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int t;
ci(t);
for(int ii=;ii<=t;ii++)
{
int n,x,ma=-N,mi=N;
memset(sum,,sizeof(sum));
ci(n);
for(int i=;i<n;i++)
ci(x),sum[x]++,ma=max(ma,x),mi=min(mi,x);
for(int i=;i<=ma;i++) sum[i]+=sum[i-];//统计小于等于i的数字的个数
for(int i=;i<=mi;i++){//从2~mi枚举gcd
a[i]=;
for(int j=i;j<=ma;j+=i){//分区间计算
int c;
if(i+j-<=ma) c=sum[i+j-]-sum[j-];
else c=sum[ma]-sum[j-];
if(!c) continue;
a[i]=(a[i]*qpow(j/i,c))%mod;
}
}
ll ans=;
for(int i=mi;i>=;i--)
{
for(int j=i+i;j<=mi;j+=i) a[i]=(a[i]-a[j])%mod;//减掉确定的倍数
a[i]=(a[i]+mod)%mod;
ans=(ans+a[i])%mod;
}
printf("Case #%d: %lld\n",ii,ans);
}
}

HDU 6053 ( TrickGCD ) 分块+容斥的更多相关文章

  1. hdu 6053 TrickGCD(筛法+容斥)

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

  2. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  3. 2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)

    题目链接 Problem Description You are given an array A , and Zhu wants to know there are how many differe ...

  4. HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法

    题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...

  5. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

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

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

  7. 2017 多校2 hdu 6053 TrickGCD

    2017 多校2 hdu 6053 TrickGCD 题目: You are given an array \(A\) , and Zhu wants to know there are how ma ...

  8. HDU 6053 TrickGCD(分块)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6053 [题目大意] 给出一个数列每个位置可以取到的最大值, 问这个可以构造多少个数列,使得他们的最 ...

  9. HDU 6053 TrickGCD(莫比乌斯反演)

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给出一个A数组,B数组满足Bi<=Ai. 现在要使得这个B数组的GCD值>=2,求共有多 ...

随机推荐

  1. 17.NET Core WebApi跨域问题

    官方说明 CORS means Cross-Origin Resource Sharing. Refer What is "Same Origin" Part Detailed P ...

  2. AngularJS(九):路由

    本文也同步发表在我的公众号“我的天空” AngularJS路由 AngularJS路由可以让我们通过不同的URL访问不同页面(似乎是废话),其价值主要体现在单页面的web应用中(single page ...

  3. mysql mysqldump 本地数据库导入本地数据库的命令

    C:\Users\Administrator>mysqldump -h localhost -P 3306 -u root -proot -n -R --triggers foryou |mys ...

  4. 从零开始的全栈工程师——js篇2.3

    自加和自减 =赋值运算 他的顺序是从右往左 从后往前 var a=12; 声明一个变量并将12赋值给aa=a+2; 将a+2赋值给a简写a+=3; a=a+3a+=1; a++ 在自己原有的基础上加1 ...

  5. 超链接显示网站 A,访问后进入网站 B

    #前端黑魔法# 出一个思考题:如何用最少的字符实现下图效果.即超链接显示网站 A,访问后进入网站 B. 当然这个是上古时代的黑魔法了,稍懂前端的都知道原理.所以这里只问最短的实现~ 一个简单的演示:( ...

  6. python super用法

    普通继承 class FooParent(object): def __init__(self): self.parent = 'I\'m the parent.' print 'Parent' de ...

  7. 再次尝试windows下msys+MinGW编译ffmpeg

    电脑上安装太多的开源库,环境变量里面一些常用的头文件都有几种,以前使用的编译ffmpeg的方法现在常常提示错误.从config.log中看,这些错误往往都是一些头文件引用错误导致.由于项目中继续编译自 ...

  8. win10蓝牙添加设备无法连接

    解决方法: 打开运行窗口,输入services.msc. 找到蓝牙支持服务(或者Bluetooth Support Service),右键,属性,启动类型选择手动,启动服务. 还不行的话,此电脑右键, ...

  9. python_图形界面编程示例

    "常用Tkinter组件的使用" #一.弹出消息框 #1 弹出提示消息框 from tkinter.messagebox import * showinfo(title='提示', ...

  10. python_26_dictionary

    #key-value 字典无下标 所以乱序,key值尽量不要取中文 info={ 'stu1101':'Liu Guannan', 'stu1102':'Wang Ruipu', 'stu1103': ...