题目链接

Problem Description

You are given an array A , and Zhu wants to know there are how many different array B satisfy the following conditions?

1≤Bi≤Ai

For each pair( l , r ) (1≤l≤r≤n) , gcd(bl,bl+1...br)≥2

Input

The first line is an integer T(1≤T≤10) 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 1≤n,Ai≤105

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

题意:

给出长度为n的A数列,求满足条件的B数组的个数,条件:①1<=b[i]<=a[i] ②对于任意区间【L,R】,区间gcd>=2

看网上大神的代码都是用莫比乌斯反演来求解的,不大理解这个,就说一下我自己的一个思路吧。

定义:

dp[i]表示gcd为i的数的个数, 则b中每个元素都为i的倍数

a数组保存每一个输进去的值,cnt[i]表示小于等于i的数的个数

设d为当前的gcd

b[i]<=a[i] 则第i个位置有a[i]/d种选择 直接累乘TLE.

若a[i]/d=k贡献为k,则和它相同贡献有cnt[kd,(k+1)d-1]个,则按段来枚举,算出该段贡献k^cnt.

最后容斥减掉gcd为jx的部分(j>1)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+20;
const ll mod=1e9+7;
ll dp[N],n,a[N],cnt[N];///dp[i]表示gcd为i的数的个数,a数组保存每一个输进去的值,cnt[i]表示小于等于i的数的个数
ll powmod(ll x,ll n)///快速幂求出x^n取模后的结果
{
ll s=1;
while(n)
{
if(n&1)
s=(s*x)%mod;
n>>=1;
x=(x*x)%mod;
}
return s%mod;
} int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
scanf("%d",&n);
memset(cnt,0,sizeof(cnt));
memset(dp,0,sizeof(dp));
ll mx=0;
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
mx=max(mx,a[i]);///mx表示输进去的这些数的最小值
cnt[a[i]]++;
}
for(int i=1; i<=mx; i++)
cnt[i]+=cnt[i-1];///cnt最终表示的是小于等于i的数的个数
ll ans=0;
for(int i=mx; i>=2; i--)///当前是以i为gcd
{
ll res=1;
if(cnt[i-1])///压根就不存在比i小的数,那么也不可能有以i为gcd的数
{
dp[i]=0;
continue;
}
for(int j=i; j<=mx; j+=i)///j都是i的倍数
{
ll num=cnt[min(mx,(ll)j+i-1)]-cnt[j-1];//[ki~(k+1)i),在这个标准下的输的个数
ll x=j/i;///贡献为x
if(num)
res=(res*powmod(x,num))%mod;///这里的含义可以理解为每个数都有x种选法,现在一共有num个数,则应该是x^num
}
dp[i]=res;
}
for(int i=mx; i>=2; i--)
{
for(int j=i+i; j<=mx; j+=i)
dp[i]=(dp[i]-dp[j]+mod)%mod;
ans=(ans+dp[i])%mod;
}
printf("Case #%d: %lld\n",++cas,ans);
}
return 0;
}

2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  2. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  3. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  4. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  5. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  6. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  7. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  8. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  9. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

随机推荐

  1. 【leetcode】59.Spiral Matrix II

    Leetcode59 Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 ...

  2. elasticsearch6 学习之基础CURD

    环境:elasticsearch6.1.2        kibana6.1.2  基础概念: 1._index元数据 (1)代表一个document存放在哪个index中(2)类似的数据放在一个索引 ...

  3. Html5新增元素中Canvas 与内联SVG的比较!

    SVG与Canvas的区别与比较如下: svg:使用xml描述2D图形,canvas使用javascript描述2D图形. Canvas 是逐像素进行渲染的,在 canvas 中,一旦图形被绘制完成, ...

  4. P4005 小 Y 和地铁

    题目描述 小 Y 是一个爱好旅行的 OIer.一天,她来到了一个新的城市.由于不熟悉那里的交通系统,她选择了坐地铁. 她发现每条地铁线路可以看成平面上的一条曲线,不同线路的交点处一定会设有 换乘站 . ...

  5. JAVA中的堆、栈等内存分析

    在 JAVA 中,有六个不同的地方可以存储数据 1. 寄存器( register ) 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...

  6. ZJOI 2017 day2 4.27

    明天就要比赛啦,今天早点休息. 既然是随便扯,首先就是yyzx的wifi(宁波的这种wifi系统我第一次见,要打开任意一个浏览器,才能跳出界面,网还是挺快的) 上午是学车的翁伊嘉&猪猪侠讲课, ...

  7. 洛谷 P2195 HXY造公园 解题报告

    P2195 HXY造公园 题目描述 现在有一个现成的公园,有\(n\)个休息点和\(m\)条双向边连接两个休息点.众所周知,\(HXY\)是一个\(SXBK\)的强迫症患者,所以她打算施展魔法来改造公 ...

  8. 【贪心】【P5078】Tweetuzki 爱军训

    Description Tweetuzki 所在的班级有 \(n\) 名学生,座号从 \(1\) 到 \(n\).有一次,教官命令班上的 \(n\) 名学生按照座号顺序从左到右排成一排站好军姿,其中 ...

  9. 使用OpenCV进行标定(转载)

    转载自牛猫靖  http://www.cnblogs.com/2008nmj/p/6278076.html 使用OpenCV进行相机标定 1. 使用OpenCV进行标定 相机已经有很长一段历史了.但是 ...

  10. poj3254 Corn Fields

    orn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17989   Accepted: 9474 Descr ...