2017 多校2 hdu 6053 TrickGCD
2017 多校2 hdu 6053 TrickGCD
题目:
You are given an array \(A\) , and Zhu wants to know there are how many different array \(B\) satisfy the following conditions?
- \(1≤B_i≤A_i\)
- 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,A_i≤10^{5}\)
Output
For the \(k\)th 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\)
思路:
枚举\(g = gcd(b_1,b_2,....,b_n)\),
那么\(gcd为g\)的倍数的答案就是\(\prod_{i=1}^{n}\frac{A_i}{g}\)
每次暴力计算是不行的,想到一个数在\(g到2g-1\)除以g结果是不变的
所以可以预处理区间数字个数的前缀和,\(O(nlogn)\)类似素数筛法预处理出每个\(g\)的答案
现在要计算\(gcd为g\)的答案,我们从大到小枚举,同时更新它的约数的答案,就可以保证不重复了
从小到大枚举过去就要用到莫比乌斯函数去计算了
\(令F(i)为gcd为i的倍数的方案数,f(i)为gcd为i的方案数\)
\(F(i) = \sum_{i|d}^{}{f(d)} \rightarrow f(i) = \sum_{i|d}u(\frac{d}{i})F(d)\)
代码贴的是比赛时过的姿势
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
vector<int> v[N];
int n;
int sum[N],ans[N];
void init(){
for(int i = 2;i < N;i++){
for(int j = i;j < N;j+=i) v[j].push_back(i);
}
}
int qpow(int x,int y){
int ans = 1;
while(y){
if(y&1) ans = 1LL* ans * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ans;
}
int main(void)
{
init();
int T, x;
int cas = 1;
cin>>T;
while(T--){
memset(sum, 0, sizeof(sum));
scanf("%d",&n);
int mi = N;
for(int i = 1;i <= n;i++) {
scanf("%d",&x);
mi = min(x,mi);
sum[x]++;
}
for(int i = 1;i < N;i++) sum[i]+=sum[i-1];
for(int i = 2;i <= mi;i++){
ans[i] = 1;
for(int j = i;j < N;j+=i){
int l = j + i - 1 > N - 1?N-1:j + i - 1;
ans[i] = 1LL * ans[i] * qpow(j / i,sum[l] - sum[j - 1]) % mod;
}
}
int res = 0;
for(int i = mi;i >= 2;i--){
res = (res + ans[i])%mod;
for(int j = 0;j < v[i].size();j++) ans[v[i][j]] = (ans[v[i][j]] - ans[i] + mod)%mod;
}
printf("Case #%d: %d\n",cas++,res);
}
return 0;
}
2017 多校2 hdu 6053 TrickGCD的更多相关文章
- HDU 6053 - TrickGCD | 2017 Multi-University Training Contest 2
/* HDU 6053 - TrickGCD [ 莫比乌斯函数,筛法分块 ] | 2017 Multi-University Training Contest 2 题意: 给出数列 A[N],问满足: ...
- 2017 多校3 hdu 6061 RXD and functions
2017 多校3 hdu 6061 RXD and functions(FFT) 题意: 给一个函数\(f(x)=\sum_{i=0}^{n}c_i \cdot x^{i}\) 求\(g(x) = f ...
- HDU 6053 TrickGCD 莫比乌斯函数/容斥/筛法
题意:给出n个数$a[i]$,每个数可以变成不大于它的数,现问所有数的gcd大于1的方案数.其中$(n,a[i]<=1e5)$ 思路:鉴于a[i]不大,可以想到枚举gcd的值.考虑一个$gcd( ...
- 2017 多校5 hdu 6093 Rikka with Number
2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...
- hdu 6053: TrickGCD (2017 多校第二场 1009) 【莫比乌斯 容斥原理】
题目链接 定义f[n]表示n是最大公约数情况下的计数,F[n]为n是公约数情况下的计数 (可以和 http://www.cnblogs.com/Just--Do--It/p/7197788.html ...
- 2017 Multi-University Training Contest - Team 2 &&hdu 6053 TrickGCD
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 6053 TrickGCD —— 2017 Multi-University Training 2
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 6053 TrickGCD 筛法
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Probl ...
- HDU 6053 TrickGCD(莫比乌斯反演)
http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给出一个A数组,B数组满足Bi<=Ai. 现在要使得这个B数组的GCD值>=2,求共有多 ...
随机推荐
- 深入理解 SVG 系列(一) —— SVG 基础
来源:https://segmentfault.com/a/1190000015652209 本系列文章分为三个部分: 第一部分是 SVG 基础. 主要讲 SVG 的一些基础知识,包括 SVG 基本元 ...
- JSP页面字符集设置
错误提示: HTTP Status 500 - /test1.jsp (line: 2, column: 1) Page directive must not have multiple occurr ...
- python3 练习题100例 (十九)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """练习十九:计算1-2+3...+99中除了88以外所有数的和" ...
- struct2 命名空间
转自http://blog.csdn.net/carefree31441/article/details/4857546 使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s: ...
- windows禁用/启用hyper-V,解决hyper-V与模拟器同时启用时造成冲突
- 5. css定位 居中
1.准备工作 (1)添加背景图片 background: url('images/grass.png') (2)背景图片格式 background-size:contain; #完全限制在方框 #co ...
- 腾讯课堂之前端开发html5css3javascriptjQueryJS年薪20万
第一章 网页制作零基础 第一节 什么是HTML 第二节 HTML基本语法 第三节 HTML结构标签 第四节 HTML常用标签及属性 第五节 HTML无序列表UL标签 第六节 HTML定义列表DL标签 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目6
2014-04-27 20:25 题目:关于java中标有synchronized的成员方法? 解法:这代表同一个对象实例的synchronized方法不能被多个线程同时调用.注意有这么多个地方都加粗 ...
- Python 字符串换行的几种方式
第一种: x0 = '<?xml version="1.0"?>' \ '<ol>' \ ' <li><a href="/pyt ...
- CSS UNIT 详解以及最佳实践
分类 ■ 绝对长度(Absolute units):cm,mm,in,pt,pc 绝对单位之间的换算:1in = 2.54cm=25.4mm=72pt=6pc 绝对长度在css中的表现和其他地方 ...