HDU6053 TrickGCD(2017多校第2场)

TrickGCD

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


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
 

—————————————————————————————————

题意:给你n个数字,每个位置的数字可以小于等于a[i],求所有gcd(l,r)都满足大于等于2的情况数

解题思路:枚举gcd的情况,每种gcd的情况等于所有a[i]/gcd的乘积,但这显然会超,所以需要优化,我们可以分块处理,如枚举5时,5 6 7 8 9对应的都是1个,所以我们可以按gcd分块,而且越大块越少,快内用快速幂加速。 得到一个dp数组dp[i]表示gcd为i的方案数,最后容斥搞一搞


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define LL long long
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
#define MAXN 100005

int pre[MAXN];
LL a[MAXN];
LL dp[MAXN];

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()
{
    int T,n;
    int q=1;
    for(scanf("%d",&T); T--;)
    {
        scanf("%d",&n);
        memset(pre,0,sizeof pre);
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            pre[a[i]]++;
        }
        for(int i=1; i<MAXN; i++) pre[i]+=pre[i-1];
        for(int i=2; i<MAXN; i++)
        {
            dp[i]=1LL;
            for(int j=0; j<MAXN; j+=i)
            {
                int cnt;
                if (j == 0) cnt = pre[j + i - 1];
                else if (j + i - 1 > 100000) cnt = pre[100001] - pre[j - 1];
                else  cnt=pre[j+i-1]-pre[j-1];

                if(j/i==0&&cnt) {dp[i]=0;break;}
                dp[i]=(dp[i]*qpow(j/i,(LL)cnt))%mod;
            }
        }
        LL ans=0;
        for(int i=a[n-1]; i>1; i--)
        {
            for(int j=i+i; j<=a[n-1]; j+=i)
            {
                dp[i]-=dp[j];
                dp[i]=(dp[i]%mod+mod)%mod;
            }
            ans+=dp[i];
            ans%=mod;
        }
        printf("Case #%d: %lld\n",q++,ans);

    }

    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值