HDU6053-TrickGCD

TrickGCD

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


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的乘积,这个需要优化,枚举除数,a[i]/gcd相同的为一块(现场的时候没想到这个),然后重复的用容斥搞一下就好了


#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 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--)
    {
        memset(sum, 0, sizeof sum);
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            int a;
            scanf("%d", &a);
            sum[a]++;
        }
        for (int i = 1; i <= 100005; i++) sum[i] += sum[i - 1];
        for (int i = 2; i <= 100000; i++)
        {
            x[i] = 1LL;
            for (int j = 0; j <= 100000; j += i)
            {
                int a, cnt;
                if (j + i - 1 > 100000) cnt = sum[100000] - sum[j - 1];
                else if (j == 0) cnt = sum[j + i - 1];
                else cnt = sum[j + i - 1] - sum[j - 1];
                a = j / i;
                if (a == 0 && cnt) x[i] = 0;
                else if (cnt) x[i] = (x[i] * qpow(a, cnt)) % mod;
            }
        }
        for (int i = 100000; i >= 2; i--)
        {
            for (int j = i + i; j <= 100000; j += i)
                x[i] -= x[j], x[i] = (x[i] % mod + mod) % mod;
        }
        LL ans = 0;
        for (int i = 2; i <= 100000; i++) ans += x[i], ans %= mod;
        printf("Case #%d: %lld\n", cas++, ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值