2017 Multi-University Training Contest - Team 2 I - TrickGCD 莫比乌斯反演

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

这篇博客讲的很详细了
我就不过多赘述了;

思路都是一样的;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
typedef __int64 ll;
typedef unsigned long long ull;
#define ms(x) memset(x,0,sizeof(x))
const long long int mod = 1e9 + 7;

inline int read()
{
    int x = 0, k = 1; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-')k = -1; c = getchar(); }
    while (c >= '0' && c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    return x * k;
}

int isprime[maxn];
int miu[maxn];
ll tot[maxn * 2];
ll num[maxn * 2];

ll quickpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b % 2)ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans;

}

void init() {


    for (int i = 1; i <= maxn; i++)miu[i] = 1;
    isprime[0] = isprime[1] = 1;
    for (int i = 2; i <= maxn; i++) {
        //if (isprime[i])continue;
        if (isprime[i] == 0) {
            miu[i] = -1;
            for (int j = 2 * i; j <= maxn; j += i) {
                isprime[j] = 1;
                if ((j / i) % i == 0)miu[j] = 0;
                else miu[j] *= -1;
            }
        }
    }
}


void init1() {
    miu[1] = 1;
    for (int i = 1; i < maxn; i++) {
        for (int j = i + i; j < maxn; j += i) {
            miu[j] -= miu[i];
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int t;
    //t = read();
    cin >> t;
    init(); 
    //init1();
    //  cout << 1 << endl;
    int cnt = 0;
    while (t--) {
        //  ms(a);
        int n; cin >> n;
        //n = read();
        cnt++;
        //ms(tot); 
        ms(num);
        int i, j;
        int minn = inf;
        for (i = 1; i <= n; i++) {
            int tmp; cin >> tmp;
            num[tmp]++;
            minn = min(minn, tmp);
        }
        ll ans = 0;
        for (i = 1; i < maxn * 2; i++) {
            tot[i] = tot[i - 1] + num[i];
        }
        for (i = 2; i <= minn; i++) {
            ll tmp = 1ll;
            if (miu[i]) {
                for (j = i; j < maxn; j += i) {
                    tmp = (tmp*quickpow(j / i, tot[j + i - 1] - tot[j - 1])) % mod;
                }

            }ans = (ans - miu[i] * tmp + mod) % mod;
        }

        cout << "Case #" << cnt << ": " << ans << endl;

        //  cout << 1 << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/81772382