莫比乌斯函数 hdu6053

本文探讨了TrickGCD问题,这是一种涉及数组操作和最大公约数(GCD)的复杂算法挑战。文中提供了详细的代码实现,包括使用莫比乌斯反演等高级数学技巧来求解特定条件下不同数组B的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TrickGCD

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


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  k th 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
#include<bits/stdc++.h>
using namespace std;
#define N 200011
const int mod=1e9+7;
typedef long long LL;
int mu[110000];
int MAXN=100000;
int a[110000];
int num[210000];
void Moblus()
{
    mu[1]=1;
    for(int i=1; i<=MAXN; i++)
        for(int j=i+i; j<=MAXN; j+=i)
            mu[j]-=mu[i];
}
LL qpow(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()
{
    Moblus();
    int t;
    scanf("%d",&t);
    for(int k=1; k<=t; k++)
    {
        int i,j,n;
        scanf("%d",&n);
        memset(a,0,sizeof(a));
        int mn=100005;
        int mx=-1;
        for(i=0; i<n; i++)
        {
            int x;
            scanf("%d",&x);
            mn=min(x,mn);
            mx=max(x,mx);
            a[x]++;
        }
        num[0]=a[0];
        for(i=1; i<=100100; i++)
                num[i]=num[i-1]+a[i];
        LL sum=0;
        int l,r;
        for(i=2; i<=mn; i++)
        {
            LL ans=1;
            for(j=1; j*i<=mx; j++)
            {
                r=(j+1)*i-1;
                l=j*i-1;
                if(r>100000)
                    r=100000;
                ans=(ans*qpow(j,num[r]-num[l]))%mod;

            }
            sum=(sum-ans*mu[i]%mod+mod)%mod;
        }
        printf("Case #%d: %lld\n",k,sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值