hdu6053
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的更多相关文章
- hdu6053 TrickGCD 容斥原理
/** 题目:hdu6053 TrickGCD 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:You are given an array ...
- HDU-6053 TrickGCD
题目连接: https://vjudge.net/problem/HDU-6053 Description You are given an array A , and Zhu wants to kn ...
- [Hdu-6053] TrickGCD[容斥,前缀和]
Online Judge:Hdu6053 Label:容斥,前缀和 题面: 题目描述 给你一个长度为\(N\)的序列A,现在让你构造一个长度同样为\(N\)的序列B,并满足如下条件,问有多少种方案数? ...
- hdu6053(莫比乌斯+容斥+分块)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意: 给出一个含 n 个元素的 a 数组, 求 bi <= ai 且 gcd(b1, ...
- Codeforces #428 Div2 D
#428 Div2 D 题意 给出一些数,现在要求找出一些数满足 \(i_1 < i_2 < i_3 < ... < i_k\) 以及 \(gcd(a_{i_1}, a_{i_ ...
- Codeforces 803F - Coprime Subsequences(数论)
原题链接:http://codeforces.com/contest/803/problem/F 题意:若gcd(a1, a2, a3,...,an)=1则认为这n个数是互质的.求集合a中,元素互质的 ...
随机推荐
- DOS程序员手册(五)
第8章磁 盘 学习编程语言,常常是从基本的输入和输出入手的(正如第5.6和第7章曾介绍的一 样).到目前为止,我们不仅学习了怎样输入和输出数据,还学习了如何进行数据操作 ...
- python的inspect模块
一.type and members 1. inspect.getmembers(object[, predicate]) 第二个参数通常可以根据需要调用如下16个方法: 返回值为object的所有成 ...
- jeakins忘记密码时的处理(简单粗暴)
1.打开config文件(通过ps -elf | grep jenkins查看JENKINS_HOME目录,然后在目录下查找config.xml文件) 2.修改<useSecurity>t ...
- Springboot 启动问题
每次以debug方式启动springboot之后都会在SilentExitExceptionHandler类中的throw new SilentExitException() 解决办法 :window ...
- 根文件系统制作、NFS配置与安装及利用NFS挂载根文件系统
最近打算从头开始制作根文件系统,下面是开发过程. 一.根文件系统的制作 0.FHS(Filesystem Hierarchy Standard)标准介绍 该标准规定了根目录下各个子目录的名称及其存放的 ...
- DataBase -- Count & Group by
SQL Count()函数: SQL COUNT(column_name)语法:COUNT(column_name)函数返回指定列的值得数目(NULL不计入) select count(column_ ...
- LeetCode -- Search a 2D Matrix & Search a 2D Matrix II
Question: Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matr ...
- [bzoj4945][Noi2017]游戏
题目大意:有$n$个位置,有三种数,每个位置只可以填一种数,$d(d\leqslant8)$个位置有三种选择,其他位置只有两种选择.有一些限制,表示第$i$个位置选了某种数,那么第$j$个位置就只能选 ...
- HDU 4910 HDOJ Problem about GCD BestCoder #3 第四题
首先 m = 1 时 ans = 0对于 m > 1 的 情况 由于 1 到 m-1 中所有和m互质的数字,在 对m的乘法取模 运算上形成了群 ai = ( 1<=a<m & ...
- BZOJ5297 [Cqoi2018]社交网络 【矩阵树定理】
题目链接 BZOJ5297 题解 最近这玩意这么那么火 这题要用到有向图的矩阵树定理 主对角线上对应入度 剩余位置如果有边则为\(-1\),不然为\(0\) \(M_{i,i}\)即为以\(i\)为根 ...