[莫比乌斯函数][分段] hdu6053 TrickGCD (2017 Multi-University Training Contest
来源:互联网 发布:手机端淘宝详情页 编辑:程序博客网 时间:2024/06/11 04:00
@(ACM题目)[莫比乌斯]
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 that1≤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
题目分析
本题给定一个长度为
-
- 整个新数组的
易得答案为:
求解该式需要以下两方面:
莫比乌斯函数
- 若
- 若
- 其他情况
我们可以莫比乌斯函数方便地进行容斥。
分段
对于上式中一个特定的
如果你蜜汁TLE了,请注意前缀和数组要开2e5而不是1e5,因为下标最大访问到
代码
#include<bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 2e5+8;const int M = 1e9+7;bitset<maxn> isPrime;int sum[maxn], primes[maxn], mu[maxn], tot = 0;void mobius(){ isPrime.set(); isPrime[1] = 0; mu[1] = 1; tot = 0; for(int i = 2; i < 100005; ++i) { if(isPrime.test(i)) primes[tot++] = i, mu[i] = -1; int d; for(int j = 0; j < tot && (d = i * primes[j]) < maxn; ++j) { isPrime[d] = false; if(i % primes[j]) mu[d] = -mu[i]; else { mu[d] = 0; break; } } }}LL pow_m(LL a, LL n, LL M){ a %= M; LL res = 1; while(n > 0) { if(n & 1) res = res*a%M; a = a * a % M; n >>= 1; } return res;}int main(){ mobius(); int T, Case = 1; cin >> T; while(T--) { int n; scanf("%d", &n); int mn = 1e5+5, mx = -1; memset(sum, 0, sizeof sum); for(int i = 0; i < n; ++i) { int x; scanf("%d", &x); ++sum[x]; if(mn > x) mn = x; if(mx < x) mx = x; } for(int i = 1; i < maxn; ++i) sum[i] += sum[i-1]; LL res = 0; for(int i = 2; i <= mn; ++i) { if(!mu[i]) continue; LL tmp = 1; for(int j = 1; i*j <= mx; ++j) tmp = tmp * pow_m(j, sum[i*j + (i-1)] - sum[i*j - 1], M) % M; res = (res - mu[i] * tmp + M) % M; } printf("Case #%d: %lld\n", Case++, res); } return 0;}
- [莫比乌斯函数][分段] hdu6053 TrickGCD (2017 Multi-University Training Contest
- 2017 Multi-University Training Contest 2 && HDOJ 6053 TrickGCD 【容斥+莫比乌斯函数】
- hdu 6053 莫比乌斯反演函数的利用 2017 Multi-University Training Contest
- hdu6053 TrickGCD 莫比乌斯函数 容斥原理
- hdu6053 TrickGCD 莫比乌斯反演
- hdu 6053 TrickGCD [2017 Multi-University Training Contest
- 【2017多校】HDU6053 TrickGCD 【莫比乌斯】
- HDU 6134 Battlestation Operational(基本数论+莫比乌斯反演)——2017 Multi-University Training Contest
- HDU6053-TrickGCD 容斥原理+莫比乌斯反演
- HDU 6053 TrickGCD(分块+容斥)——2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- 2017 Multi-University Training Contest
- HDU-6048 Puzzle(思维题)
- 对ioc的理解
- Collections
- PowerMock与Mockito的使用
- 二叉树--由前序遍历和中序遍历重建二叉树
- [莫比乌斯函数][分段] hdu6053 TrickGCD (2017 Multi-University Training Contest
- Maven:安装过程以及在eclipse上的使用
- 之前写的扯淡定时执行
- Android 双开沙箱 VirtualApp 源码分析(六)ContentProvider
- 工作日常用到的一些命令
- Java多线程之生产者和消费者模式解析
- 646. Maximum Length of Pair Chain
- 华为OJ——查找输入整数二进制中1的个数
- centos下安装mongo数据库及php扩展