HDU 6053 TrickGCD(分块+容斥)——2017 Multi-University Training Contest

来源:互联网 发布:泰拉瑞亚骸骨魔弓数据 编辑:程序博客网 时间:2024/05/14 05:00

传送门

TrickGCD

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


Problem Description
You are given an array A , and Zhu wants to know there are how many different array B satisfy the following conditions?

* 1BiAi
* For each pair( l , r ) (1lrn) , gcd(bl,bl+1...br)2
 

Input
The first line is an integer T(1T10) 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 1n,Ai105
 

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

题目大意:
给了一个数列 A, 现在有一个数列 B 满足 1BiAi,满足区间 [1,n], GCD(B1,B2,...,Bn)1, 求这样的数列有多少个。

解题思路:
其实正常的思路就是想到枚举 gcd 值,然后计算结果,但是发现这个比较不好求,所以不可行。所以考虑 di 表示 gcd为 i 的倍数的有 di 个,每个di对答案的贡献是 di,然后我们发现这样计算重复了好多, 考虑容斥。因为 我们计算的是 gcd 为 i 的值,然而现在已经求得了 di,那么我们需要计算的结果就是 gcd 为 i 的倍数的值,也就是 di 减去 gcd 为 ij(j>1) 的倍数的值。
现在考虑如何计算 di,其实就是用到了分块,因为我们发现在 k[ji,(j+1)i1] 区间中,ki 的值都是一样的,根据这个原理,我们可以求这个区间中有多少个 i,然后快速幂一下就可以了,当然这个东西先求一个前缀和 sum[i]表示,A 数列中 i 的有 sum[i] 个。
代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;const LL MOD = 1e9+7;const int MAXN = 1e5+15;LL sum[MAXN], d[MAXN];LL Pow(LL a, LL b){    LL ans = 1;    while(b){        if(b & 1) ans = ans * a % MOD;        b>>=1;        a = (a * a) % MOD;    }    return ans;}int main(){    ///freopen("in.txt","r", stdin);    int T; scanf("%d", &T);    for(int cas=1; cas<=T; cas++){        int n, x, cnt; scanf("%d", &n);        int mi = MAXN, ma = -1;        memset(sum, 0, sizeof sum);        for(int i=0; i<n; i++) scanf("%d", &x), mi = min(mi, x), ma = max(ma, x), sum[x]++;        for(int i=1; i<=ma; i++) sum[i] += sum[i-1];        for(int i=2; i<=ma; i++){            d[i] = 1;            for(int j=i+i; j<=ma; j+=i){                if(i+j-1 > ma) cnt = sum[ma] - sum[j-1];                else cnt = sum[i+j-1] - sum[j-1];                if(cnt == 0) continue;                d[i] = d[i] * Pow(j/i, cnt) % MOD;            }        }        LL ans = 0;        for(int i=mi; i>1; i--){//一定是从后往前计算            for(int j=i+i; j<=mi; j+=i) d[i] = (d[i] - d[j]) % MOD;            ans = (ans + d[i]) % MOD;        }        ans = (ans % MOD + MOD) % MOD;        printf("Case #%d: %lld\n",cas, ans);    }    return 0;}
阅读全文
0 0
原创粉丝点击