1. 程式人生 > >HDU 6053 TrickGCD (桶裝+分段 / 莫比烏斯反演)

HDU 6053 TrickGCD (桶裝+分段 / 莫比烏斯反演)

TrickGCD

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


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 ) (1
lrn
) , 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 m
od
 109+7
Sample Input 1 4 4 4 4 4
Sample Output Case #1: 17
Source
Recommend liuyiding   |   We have carefully selected several similar problems for you:  6055 6054 6053 6052 6051 

思路來源:http://www.cnblogs.com/jhz033/p/7246028.html

題意:給你n個數字,每個位置的數字可以小於等於a[i],求所有gcd(l,r)都滿足大於等於2的情況數; 思路:顯然列舉gcd的情況,對於每個位置都有a[i]/gcd的個數可以滿足條件,gcd的情況的所有a[i]/gcd的乘積;    這個也需要優化,列舉除數,a[i]/gcd相同的為一塊,nlong(n)的複雜度*快速冪的log,後面的用容斥篩一下就好了; 這個題是向下取整,所以每一塊長度就是要除的i本身大小,但如果是向上取整就比較複雜了,下面部落格D題有介紹
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const int Mod = 1e9 + 7;
int a[maxn], ans[maxn], cnt[maxn], num[maxn];
ll quick_mod(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b&1) ans = ans*a%Mod;
        b /= 2;
        a = a*a%Mod;
    }
    return ans;
}
int main()
{
    int n, t, ca = 1, x;
    scanf("%d", &t);
    while(t--)
    {
        memset(a, 0, sizeof(a));
        memset(cnt, 0, sizeof(cnt));
        memset(ans, 0, sizeof(ans));
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)  //桶裝
        {
            scanf("%d", &x);
            a[x]++;
        }
        for(int i = 1; i < maxn; i++)
            cnt[i] = cnt[i-1]+a[i];
        for(int i = 2; i < maxn; i++)  //列舉gcd
        {
            num[i] = 1;
            for(int j = 0; j < maxn; j += i)  //分段
            {
                int res, limit = j+i-1 >= maxn ? maxn-1 : j+i-1;
                if(!j) res = cnt[limit];
                else res = cnt[limit]-cnt[j-1];
                int tmp = j/i;
                if(!tmp && res) num[i] = 0;
                else if(res) num[i] = num[i]*quick_mod(tmp, res)%Mod;
            }
        }
        for(int i = maxn-1; i >= 2; i--)  //容斥
        {
            ans[i] = num[i];
            for(int j = i+i; j <= maxn-1; j += i)  //容斥
                ans[i] = ((ans[i]-ans[j])%Mod+Mod)%Mod;
        }
        ll res = 0;
        for(int i = 2; i <= maxn-1; i++)
            res += ans[i], res %= Mod;
        printf("Case #%d: %lld\n",ca++,res);
    }
    return 0;
}



莫比烏斯程式碼待更新..

TrickGCD

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


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
Source
Recommend liuyiding   |   We have carefully selected several similar problems for you:  6055 6054 6053 6052 6051