Online JudgeHdu6053

Label:容斥,前缀和

题面:

题目描述

给你一个长度为\(N\)的序列A,现在让你构造一个长度同样为\(N\)的序列B,并满足如下条件,问有多少种方案数?答案对\(1e9+7\)取模。

  • \(1≤Bi≤Ai\)

  • 对于任意(l,r) \((1≤l≤r≤N)\),有\(gcd(b_l,b_{l+1}...b_r)>=2\)

输入

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≤10^5\)

输出

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 \(10^9+7\)

样例

Input

1
4
4 4 4 4

Output

Case #1: 17

题解

一、暴力做法:\(O(N \cdot(ln(N)+N))\)

这题的暴力做法很容易想到,直接枚举\(gcd\),统计该值对答案的贡献。对于序列A中的第i个数,此时他可取的数字有\(Ai/gcd\)种,则此时整个序列的方案数为\(Ans=A_1/gcd *A_2/gcd \cdot...* A_N/gcd\)。

当然还需要容斥,我们从大到小枚举这个\(gcd\),然后再减去\(gcd\)的倍数对答案的贡献即可。

二、AC做法:\(O(N \cdot ln(N))\)

上面做法的瓶颈在于直接O(N)地枚举整个A序列来求该\(gcd\)对答案的贡献。

仍然是枚举\(gcd\),发现\([i \cdot gcd,(i+1) \cdot gcd-1]\)这个区间内的数在此时的贡献都是\(i\)。想到直接枚举\(gcd\)的倍数即可,而查一段区间内的数字个数可以用前缀和完成(因为序列数字的上限只有1e5)。对于每个\(gcd\),之前的暴力做法是全部累乘,而这里用一下快速幂就好了。

完整代码如下:

#include<bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;
const int N=1e5+10;
int n,a[N],f[N];
int dp[N];
int ksm(int a,int d){
int res=1;
while(d){
if(d&1)res=res*a%mod;
a=a*a%mod;d>>=1;
}
return res;
}
signed main(){
int T,cas=0;scanf("%lld",&T);
while(T--){
scanf("%lld",&n);
memset(f,0,sizeof(f));
memset(dp,0,sizeof(dp)); int ma=0,ans=0;
for(int i=1;i<=n;i++)scanf("%lld",&a[i]),ma=max(ma,a[i]),f[a[i]]++;
for(int i=1;i<=ma;i++)f[i]+=f[i-1];
for(int i=ma;i>=2;i--){
int cur=1;
for(int ti=0,j=0;j<=ma;ti++,j+=i){
int num=f[min(j+i-1,ma)];
if(j!=0)num-=f[j-1];
cur=cur*ksm(ti,num)%mod;
}
for(int j=i+i;j<=ma;j+=i)cur=(cur-dp[j]+mod)%mod;
ans=(ans+(dp[i]=cur))%mod;
}
printf("Case #%lld: %lld\n",++cas,(ans+mod)%mod);
}
}

[Hdu-6053] TrickGCD[容斥,前缀和]的更多相关文章

  1. 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 ...

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

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

  3. hdu 5514 Frogs(容斥)

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. HDU 5213 分块 容斥

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

  5. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

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

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

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

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

  8. HDU 6053 ( TrickGCD ) 分块+容斥

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

  9. HDU 6053 TrickGCD(分块)

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

随机推荐

  1. AndroidStudio WiFi调试插件

    前言 此篇博客也是Android studio插件篇的一部分,后续有时间我会介绍更多AndroidStudio的插件方便开发. Android设备用WiFi调试在以前一般是通过adb连接的,但是这样的 ...

  2. [JZOJ6340] 【NOIP2019模拟2019.9.4】B

    题目 题目大意 给你个非负整数数列\(a\),每次等概率选择大于零的\(a_i\),使其减\(1\). 问\(a_1\)被减到\(0\)的时候期望经过多少次操作. 思考历程 对于这题的暴力做法,显然可 ...

  3. jupyter|魔法函数问题| UsageError: Line magic function `%` not found

    问题: jupyter notebook 使用魔法函数% matplotlib inline,报错:UsageError: Line magic function `%` not found 解决: ...

  4. 页面JS缓存问题解决方案

    .在jsp中加入头 <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUI ...

  5. sublime 3打开中文乱码问题

    首先到官网 https://packagecontrol.io/installation#Simple 下载一个控制台支持的扩展包Package Control.sublime-package 在su ...

  6. linux下怎么安装Go开发环境?linux部署golang

    linux下怎么安装Go开发环境?linux部署golang  0.请自行安装SSH远程工具 1.  SSH远程登录你的linux服务器 2.   yum install mercurial安装 me ...

  7. 使用Windbg调试系统弹出的内存不可读错误

    步骤: 1. 使用Windbg挂钩到崩溃的进程上面 2. 使用~*k列出所有线程 3. 搜索UnhandledExceptionFilter所在的线程 4. 使用~ns切换到上面崩溃所在的线程,n为线 ...

  8. R语言中的线性判别分析_r语言 线性判别分析

    R语言中的线性判别分析_r语言 线性判别分析 在R语言中,线性判别分析(Liner Discriminant Analysis,简称LDA),依靠软件包MASS中有线性判别函数lqa()来实现.该函数 ...

  9. 在ubuntu下编写python

    一般情况下,ubuntu已经安装了python,打开终端,直接输入python,即可进行python编写. 默认为python2 如果想写python3,在终端输入python3即可. 如果需要执行大 ...

  10. JDK语法糖之switch字串与枚举支持

    在JDK1.7之前,switch只支持byte,short,char,int,注意1.5之后的自动拆箱,对应的这四种基础类型的封装类也同样支持Byte,Short,Character,Integer, ...