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. Maven的学习资料收集--(六) 构建Hibernate项目

    前面我们使用Maven构建了Struts2项目,这里我们来试一下Hibernate项目: 这里的例子,大体框架应该是正确的,但是,对于Maven的很多约定都没有掌握,估计包的命名都不是非常好,等以后, ...

  2. SpringBoot | 第二十六章:邮件发送

    前言 讲解了日志相关的知识点后.今天来点相对简单的,一般上,我们在开发一些注册功能.发送验证码或者订单服务时,都会通过短信或者邮件的方式通知消费者,注册或者订单的相关信息.而且基本上邮件的内容都是模版 ...

  3. idea没有绑远程地址,如何提交到github的空项目

    一 有同事问我怎么提交到github的空项目....这么简单.... 二 1.创建本地的git仓库 选择自己的项目 创建成功 2.添加代码,选中左边的文件,add 3.提交 5.定义远程仓库,不定义的 ...

  4. C# 多线程之线程控制

    方案一: 调用线程控制方法.启动:Thread.Start();停止:Thread.Abort();暂停:Thread.Suspend();继续:Thread.Resume(); private vo ...

  5. java(itext) 一个很简单的PDF表格生成工具

    先上个效果图 因为做的项目涉及到数据预测,其中有大量打印业务来支撑实体店的运营,因为注重的是数据,要求简洁,清晰,所以写了个很简单也很实用的工具类. 如果需要编写样式或者插入背景,都可以查阅itex官 ...

  6. 命名空间namespace、smarty使用(视图分离,MVC)、smarty模板语法、smarty缓存、MVC模式

    一.命名空间:namespace 命名空间 可以理解为逻辑上的使用,为了防止重名 namespace :关键字 加载:require_once();//加载一次 include_once() 申明命名 ...

  7. es6-Module 的加载实现

    浏览器加载 传统方法 在 HTML 网页中,浏览器通过<script>标签加载 JavaScript 脚本. <!-- 页面内嵌的脚本 --> <script type= ...

  8. 构建第一个Spring Boot2.0应用之application.properties和application.yml(八)

    本节学习在项目中配置文件配置的方式,一种是通过applicaiton.properties,一种是通过application.yml方式. 一.环境: IDE:IntelliJ IDEA 2017.1 ...

  9. .NET中异常类(Exception)

    异常:程序在运行期间发生的错误.异常对象就是封装这些错误的对象. try{}catch{}是非常重要的,捕获try程序块中所有发生的异常,如果没有捕获异常的话,程序运行的线程将会挂掉,更严重的是这些错 ...

  10. thinkphp分页+条件查询

    最近项目上面有一个带条件查询的分页列表,一开始form用的post,点击第二页就没有跳转成功,原因是分页是get请求,post数据链接到其他页面就会被清除. 解决办法: 1.form表单method= ...