TrickGCD

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

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
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6066 6065 6064 6063 6062 
 
 

看范围就知道要去枚举gcd,对于每个gcd,在a[i]这个位置上有gcd/a[i]个数能满足条件构成b[i],只要把每个位置求出来累乘就好了,但是这个过程还需要优化。

优化可以用素数筛法类似的方式,把gcd/a[i]相同的数都一起处理,这样在用一个快速幂把相同的那些情况数算出来,时间复杂度就是n*logn*logn。

但是答案显然会有重复,赛时就是这个问题不会解决然后gg。

dp[i]表示 gcd=i 时求出的符合情况数,显然它包含了 i 的倍数对应的情况,我们需要把这些数减去,但是倍数的情况显然也有重复,比较复杂。

这个过程我们可以倒过来从上界往下去容斥,这样每次对于num[i]来说,它的倍数的情况都是已经处理好了的,只要依次减去倍数的情况了,其实相当于一个dp的过程。。

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cmath>
using namespace std; const long long mod=1e9+;
int sum[],a[];
long long dp[]; int n,T,k;
long long poww(long long a, long long b)
{
long long ans=;
while(b)
{
if (b&) ans=(ans*a)%mod;
b>>=;
a=(a*a)%mod;
}
return ans;
}
int main()
{
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
memset(sum,,sizeof(sum)); scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[a[i]]++;
}
for(int i=;i<=1e5;i++) sum[i]+=sum[i-]; for(int i=;i<=1e5;i++) // gcd=i
{
dp[i]=;
for(int j=;j<=1e5;j+=i)
{
if (j+i->) k=sum[]-sum[j-];
else if (j==) k=sum[+i-];
else k=sum[j+i-]-sum[j-]; //k表示a序列中,数字范围在[j,j+i-1]的个数,分段
int knum=j/i; // 该范围的数字,对于gcd=i的倍数的个数相同 if (knum== && k>) dp[i]=; //如果这范围的数字存在,但是,要gcd==i没有数字可选,那么这个gcd无解,不存在可行解
else dp[i]=(dp[i]*poww(knum,k))%mod;
}
} for(int i=1e5;i>=;i--)
for(int j=i+i;j<=1e5;j+=i)
{
dp[i]-=dp[j];
dp[i]=(dp[i]+mod)%mod;
}
long long ans=;
for(int i=;i<=1e5;i++)
ans=(ans+dp[i])%mod;
printf("Case #%d: %lld\n",cas,ans);
}
return ;
}

hdu 6053 TrickGCD(筛法+容斥)的更多相关文章

  1. hdu 6053 trick gcd 容斥

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

  2. hdu 6053 TrickGCD 筛法

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

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

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

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

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

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

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

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

  9. HDU 6053 TrickGCD(分块)

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

随机推荐

  1. Python面试题之Python反射机制

    0x00 前言 def f1(): print('f1') def f2(): print('f2') def f3(): print('f3') def f4(): print('f4') a = ...

  2. MVC 4中的前端渲染 @Helper指令

    如果我们需要在一个页面或多个页面显示如人民币格式(后台传回来的无¥)¥的格式化.或是对后台数据作如保留小数个数等处理,这些东西经常要用到,特别是一些NULL值的处理,有可能会出错.这时我们可以通过创建 ...

  3. 20135302魏静静——linux课程第五周实验及总结

    linux课程第五周实验及总结 一.学习总结 给MenuOS增加time和time-asm命令(四步操作命令) rm menu -rf 强制删除git clone http://github.com/ ...

  4. strcpy、sprintf、memcpy的区别

    char*strcpy(char *dest, const char *src); 其对字符串进行操作,完成从源字符串到目的字符串的拷贝,当源字符串的大小大于目的字符串的最大存储空间后,执行该操作会出 ...

  5. Jquery6 DOM 节点操作

    学习要点: 1.创建节点 2.插入节点 3.包裹节点 4.节点操作 DOM 中有一个非常重要的功能,就是节点模型,也就是 DOM 中的“M”.页面中的元素结构就是通过这种节点模型来互相对应着的,通过这 ...

  6. wechat4j获取用户昵称乱码修复

    项目对接微信公众号平台时,微信的官方给出的建议是使用wechat4j.官方建议的,自然心里踏实,但实际用起来时发现wechat4j埋有很多雷,最让人心烦意乱的就是中文乱码问题. 之前写过一篇为JAXB ...

  7. CentOS7系统安装及环境初始化

    操作系统安装:    将网卡名称设置为eth*,不使用CentOS 7默认的网卡命名规则.所以需要在安装的时候,增加内核参数.1. 光标选择“Install CentOS 7” 2. 点击Tab,打开 ...

  8. mysql创建数据库和用户

    create database sonar character set utf8 collate utf8_general_ci; flush privileges; grant all privil ...

  9. Animal_human_kp人脸与马脸迁移学习GitHub 论文实现

    Interspecies Knowledge Transfer for Facial Keypoint Detection关键点检测   Github地址:Interspecies Knowledge ...

  10. 数据结构实习 problem O Huffman Tree

    Huffman Tree 题目描述 对输入的英文大写字母进行统计概率 然后构建哈夫曼树,输出是按照概率降序排序输出Huffman编码. 输入 大写字母个数 n 第一个字母 第二个字母 第三个字母 .. ...