hdu 6053

TrickGCD

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


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
 
比赛的时候写了一个o(n^2)的算法超时了一直也没找到解决办法,看了题解之后就懂了,当我们要除以一个 i  的时候,有好多数/i的商是一样的这些数就可以一块计算,这样的话就把o(n)的算法变为o(log n)

题意:给你一个数组a,让我们确定b数组可以的情况数,当然数组b要满足题目中的两个条件

思路:首先我们可以枚举gcd的值,然后每个gcd对结果的贡献就是a[i]/gcd的乘积(这里就用到一开始提到的算法把o(n)降为o(log n)),但是这样算的话会出现大量重复,但是只需要简单的容斥就可以解决了

ac代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>


using namespace std;


#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod = 1000000007;


int sum[100009], n;
LL x[100009];
LL phi[100005];


LL qpow(LL x, LL y)
{
    LL ans = 1;
    while (y)
    {
        if (y & 1) ans = (ans*x) % mod;
        y >>= 1;
        x = (x*x) % mod;
    }
    return ans;
}


int main()
{
    int t, cas = 1;
    scanf("%d", &t);
    while (t--)
    {
        int  n,m;
        cin>>n;
        memset(sum,0,sizeof(sum));
        memset(x,0,sizeof(x));
        int max1=-1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&m);
            sum[m]++;
            max1=max(max1,m);
        }
        for(int i=1;i<=max1;i++)
        sum[i]+=sum[i-1];
        for(int i=2;i<=max1;i++)
        {
            x[i]=1;
            if(sum[i-1])
            {
                x[i]=0;
                continue;
            }
            int yh=0;
            for(int j=i;j<=max1;j+=i)
            {
                int min1=min(max1,j+i-1);
               int ph=sum[min1]-sum[j-1];
               yh+=ph;
               int b=j/i;
               x[i]=x[i]*qpow((long long)b,(long long)ph)%mod;
            }
            if(yh!=n)
            x[i]=0;
        }
        for(int i=max1;i>=2;i--)
        {
            for(int j=i+i;j<=max1;j+=i)
            {
                x[i]=(x[i]-x[j]+mod)%mod;
            }
        }
        long long ans=0;
        for(int i=2;i<=max1;i++)
        ans=(ans+x[i])%mod;
        printf("Case #%d: %lld\n",cas++,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值