HDU 6053 TrickGCD (桶装+分段 / 莫比乌斯反演)

49 篇文章 0 订阅
37 篇文章 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  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
 

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  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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6055  6054  6053  6052  6051 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值