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. Python3基础(3)集合、文件操作、字符转编码、函数、全局/局部变量、递归、函数式编程、高阶函数

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  2. Java8 如何正确使用 Optional

    原文出处:https://blog.kaaass.net/archives/764 Optional是Java8提供的为了解决null安全问题的一个API.善用Optional可以使我们代码中很多繁琐 ...

  3. hibernate课程 初探单表映射1-8 hibernate持久化类

    java beans 的设计原则 1 公有的类 2 共有不带参数构造方法 3 私有属性 4 属性setter/getter方法 Studnet类: package com.ddwei.student; ...

  4. 【精华】9张思维导图带你学习Javascript

    转自:ChokCoco(http://www.cnblogs.com/coco1s/p/3953653.html) 学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javasc ...

  5. Garmin APP开发之入门

    Garmin开发-入门 先附上几个已经开发完成的app日历 up down 翻月 start 回到当前月(就差农历了) 秒表和定时器一体app界面比较简单,但是实用,长按菜单键可以切换秒表和定时器,有 ...

  6. linux书籍推荐

    <Linux/Unix设计思想> 图书将Unix与Linux的原理有效地结合起来,总结了Unix/Linux软件开发中的原则.在保留了第1版中Unix方面的内容的同时,强调了Linux和开 ...

  7. ASP.NET中登陆验证码的生成和输入验证码的验证

    一:验证码的生成实现代码 protected void Page_Load(object sender, EventArgs e)    {        string validateCode = ...

  8. Mac终端下使用***

    首先安装proxychains: brew install proxychains-ng 然后创建文件~/.proxychains/proxychains.conf,写入以下内容: strict_ch ...

  9. JAVA设计模式初探之桥接模式

    生活中的一个例子:    拿汽车在路上行驶的来说.既有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶.这你会发现,对于交通工具(汽车)有不同的类型,它们所行驶的环境(路)也 ...

  10. Linux 开启关闭防火墙

    开放防火墙端口添加需要监听的端口 /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT/sbin/iptables -I INPUT -p tcp ...