hdu6053

题意

给出 \(A\) 数组,问有多少种 \(B\) 数组满足下面条件。

  • \(1≤ B_i ≤ A_i\)
  • For each pair \(( l , r ) \ (1≤l≤r≤n) , gcd(b_l,b_{l+1}...b_r) ≥ 2\) 。

分析

首先肯定要去枚举 \(gcd\) ,如果暴力去计算,对于每个 \(gcd\) ,我们都要乘 \(n\) 次,这样显然会超时。考虑一种将区间分块的思想,如果 \(gcd\) 为 \(10\) ,那么区间 \([20, 30)\) 里的数除以 \(10\) 都是 \(2\) ,当 \(gcd\) 越大时,区间越大。我们直接统计下前缀和,可以查询某个区间里包含的数的个数,快速幂计算答案。

最后求得的 \(dp[i]\) 表示 \(gcd=i\) 时,构成的 \(B\) 数组的个数,可以用容斥去处理得到最后的答案。

code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int N = 1e5 + 5;
const ll MOD = 1e9 + 7;
int kase = 1;
int T;
ll POW(ll x, int n) {
ll res = 1;
while(n) {
if(n & 1) res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
}
int sum[MAXN];
ll dp[MAXN];
int main() {
scanf("%d", &T);
while(T--) {
memset(sum, 0, sizeof sum);
memset(dp, 0, sizeof dp);
int n;
scanf("%d", &n);
int mn = N;
for(int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
mn = min(mn, x);
sum[x]++;
}
for(int i = 1; i <= N; i++) {
sum[i] += sum[i - 1];
}
for(int i = 2; i <= mn; i++) {
ll c = 0;
dp[i] = 1;
for(int j = i; j <= N; j += i) {
c++;
int x;
if(j + i - 1 > N) x = sum[N] - sum[j - 1];
else x = sum[j + i - 1] - sum[j - 1];
if(x == 0) continue;
dp[i] = (dp[i] * POW(c, x)) % MOD;
}
}
for(int i = N; i >= 2; i--) {
for(int j = 2 * i; j <= N; j += i) {
dp[i] = (dp[i] - dp[j] + MOD) % MOD;
}
}
ll ans = 0;
for(int i = 0; i <= N; i++) {
ans = (ans + dp[i]) % MOD;
}
printf("Case #%d: %lld\n", kase++, ans);
}
return 0;
}

hdu6053的更多相关文章

  1. hdu6053 TrickGCD 容斥原理

    /** 题目:hdu6053 TrickGCD 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:You are given an array ...

  2. HDU-6053 TrickGCD

    题目连接: https://vjudge.net/problem/HDU-6053 Description You are given an array A , and Zhu wants to kn ...

  3. [Hdu-6053] TrickGCD[容斥,前缀和]

    Online Judge:Hdu6053 Label:容斥,前缀和 题面: 题目描述 给你一个长度为\(N\)的序列A,现在让你构造一个长度同样为\(N\)的序列B,并满足如下条件,问有多少种方案数? ...

  4. hdu6053(莫比乌斯+容斥+分块)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意: 给出一个含 n 个元素的 a 数组, 求 bi <= ai 且 gcd(b1, ...

  5. Codeforces #428 Div2 D

    #428 Div2 D 题意 给出一些数,现在要求找出一些数满足 \(i_1 < i_2 < i_3 < ... < i_k\) 以及 \(gcd(a_{i_1}, a_{i_ ...

  6. Codeforces 803F - Coprime Subsequences(数论)

    原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...

随机推荐

  1. DOS程序员手册(六)

    217页 程序的主要部分后面是主程序所使用的许多小的扩充内存功能.将这些功能组合起 来这些功能便覆盖了扩充内存的操作,尽管还可能想向它们添加错误检查. 程序所包含的函数有: emmtest   检验内 ...

  2. google protobuf 中的proto文件编写规则

    1. 简单介绍 protobuf文件:就是定义你要的消息(类似Java中的类)和消息中的各个字段及其数据类型(类似java类中的成员变量和他的数据类型) 2. Protobuf消息定义 消息由至少一个 ...

  3. ubuntu 16.04 安装grpc

    参考自:http://dreamlikes.cn/archives/555 ==== 其中在第四步,编译安装gRPC时, make 后,出现错误 /usr/bin/ld: warning: libpr ...

  4. Java Web Action DAO Service层次理解

    参考来源:http://blog.csdn.net/inter_peng/article/details/41021727 1. Action/Service/DAO简介: Action是管理业务(S ...

  5. 重复造轮子系列--dijkstra算法

    前年一时脑热(理想很丰满,现实很骨感),写了这个最短路径优先的低效版本,且留着回忆吧. spf.h #ifndef SPF_H_ #define SPF_H_ typedef struct { int ...

  6. asp.net 存储过程 输出参数 取不到值

    这是MSDN上的明确解释:当您将 Command 对象用于存储过程时,可以将 Command 对象的 CommandType 属性设置为 StoredProcedure.当 CommandType 为 ...

  7. 设计模式之策略模式的Python实现

    1. 策略模式解决的是什么问题 策略模式解决的应用场景是这样的: 在业务场景中,需要用到多个算法,并且每个算法的参数是需要调整的.那么当不同的行为堆砌到同一个类中时,我们很难避免使用条件语句来选择合适 ...

  8. 修复linux密码

    To reset the root password of your server, you will need to boot into single user mode. Access the M ...

  9. mac平台打造犀利的Android Studio开发环境

    0x0 背景介绍  随着Android Studio功能越来越强大,Android平台的开发者们基本上都从原来的Eclipse + ADT 转到了AS上.本文就记录自己在配置AS环境过程中遇到的各种问 ...

  10. jquery 的ajax

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...