2017多校第二场1009(容斥)

TrickGCD

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

Problem 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 that 1≤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

解题思路:容易想到可以枚举gcd,然后对于每个数满足条件的有a[i] / gcd个(会重复,后面用容斥去掉),然后把每个数乘起来就行。

但是如果单纯这样暴力做,复杂度是n * n ,所以我们换一种方法,我们把a[i] / gcd相同的分为一组,这样就快多了。最后用容斥处理一下就行。

为什么要用容斥,也就是重复的地方在哪里,举个例子
8 8 8 8四个数,我们枚举gcd = 2时,算的是(8 / 2) * (8 / 2) * (8 / 2)*(8/2),所以肯定用4, 4, 4,4 这种组合,显然这样gcd = 4不是2.所以我们要减去所以为2的倍数的那一部分。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
int a[maxn];//原数组中元素为i的有多少个
int sum[maxn];//前缀和
ll num[maxn];//容斥之前gcd为i的种类数
ll ans[maxn];//容斥之后gcd为i的种类数
int N;
void init()
{
    memset(a, 0, sizeof(a));
    memset(num, 0, sizeof(num));
    memset(ans, 0, sizeof(ans));
}
ll quick_mod(int i, ll x)
{
    ll Ans = 1;
    x %= mod;
    while(i)
    {
        if(i&1) Ans = (Ans * x) % mod;
        i >>= 1;
        x = (x * x) % mod;
    }
    return Ans;
}
int main()
{
    int T;
    int Case = 1;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        init();
        int x;
        for(int i = 1; i <= N; i++)
        {
            scanf("%d", &x);
            a[x]++;
        }
        sum[0] = 0;
        for(int i = 1; i < maxn; i++)
        {
            sum[i] = sum[i - 1] + a[i];
        }
        for(int i = 2; i < maxn; i++)//枚举gcd
        {
            num[i] = 1;
            for(int j = 0; j < maxn; j += i)//把arr[i] / gcd相同的分为一块
            {
                int up;
                int res = j + i - 1;
                if(res >= maxn) up = maxn - 1;
                else up = j + i - 1;
                int Count;
                if(j == 0) Count = sum[up];
                else Count = sum[up] - sum[j - 1];
                ll term = (ll)j / (ll)i;
                if(!term && Count) num[i] = 0;
                else num[i] = ((num[i] % mod) * quick_mod(Count, term)) % mod;
            }
        }
        //进行容斥
        for(int i = maxn - 1; i >= 2; i--)
        {
            ans[i] = num[i];
            for(int j = i + i; j < maxn; j += i)
            {
                ans[i] = ((ans[i] - ans[j] + mod)) % mod;
            }
        }
        ll res = 0;
        for(int i = 2; i < maxn; i++)
        {
            res = (res + ans[i]) % mod;
        }
        printf("Case #%d: %lld\n", Case++, res);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值