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. 初识Qgis

    折腾了一天,qgis终于能在跟了自己8年的本本上顺利打开了,官网先后下载了3.8和3.4版本的都出现了同样的问题,"could not load qgis_app.dll",goo ...

  2. SpringBatch批处理框架:入门项目

    1.项目结构如下:

  3. Date()日期转换和简单计算

    /** * 判断是否为闰年 * @param year * @return */ public boolean isLeap ( int year ) { if ( (year % 4 == 0 &a ...

  4. oracle将查询结果横转纵

    SELECT '残疾人|民政|综合治理|计划生育|物业监管|安全生产|环境类|司法信访|党建|社会组织|文化体育|社保' D , '53|52|51|50|49|48|47|5|4|3|2|1' g ...

  5. Parse:App开发必备 让应用开发效率提高上百倍

    Parse一个应用开发工具, 是由Y Combinator所孵化的创业公司.使用Parse能把效率提高10倍到100倍.通常情况下,从开发用户到推广用户需要花几周时间,用了Parse则只需几小时.[U ...

  6. FTP Active & Passive

          在主动模式下,FTP客户端随机开启一个大于1024的端口N向服务器的21号端口发起连接,然后开放N+1号端口进行监听,并向服务器发出PORT N+1命令.服务器接收到命令后,会用其本地的F ...

  7. swoole手册

    https://wiki.swoole.com/wiki/ Swoole Github项目地址:https://github.com/swoole/swoole-src/ (支持请点Star) 开源中 ...

  8. Redis-GEO

    一. Redis的GEO特性 Redis3.2版本提供了GEO功能,支持存储地理位置信息用来实现诸如摇一摇,附近位置这类依赖于地理位置信息的功能.二. 命令2.1 增加地理位置信息 命令:geoadd ...

  9. 关于C++ const 的全面总结 分类: ubuntu 2014-12-03 21:03 72人阅读 评论(0) 收藏

    C++中的const关键字的用法非常灵活,而使用const将大大改善程序的健壮性,本人根据各方面查到的资料进行总结如下,期望对朋友们有所帮助. Const 是C++中常用的类型修饰符,常类型是指使用类 ...

  10. 使用maven搭建Hibernate

    使用maven搭建Hibernate框架(web项目) create table USERS ( ID NUMBER not null primary key, NAME ), PASSWORD ), ...