2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)
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 (容斥公式)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
- 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− ...
随机推荐
- CentOS7 修改yum源为阿里云
1,登陆root帐号 2,cd /etc/yum.repo.d 3,mv CentOS-Base.repo CentOS-Base.repo.bak4,wget http://mirrors.aliy ...
- Spring-MVC理解之二:前置控制器
原文链接:http://www.cnblogs.com/brolanda/p/4265749.html 一.前置控制器配置与讲解 上篇中理解了IOC容器的初始化时机,并理解了webApplicatio ...
- Android Studio- 把项目提交到SVN中操作方法
第一步 下载SVN,下载完成之后,需要吧command line client tools点击修改安装 然后Crash Reporter点击选择取消安装 如果不进行该操作,则可能在C:\Program ...
- BZOJ5073 小A的咒语(动态规划)
设f[i][j][0/1]为前i位选j段时其中第i位选/不选最多能匹配到哪,转移时f[i][j][0]→f[i+1][j][0],f[i][j][1]→f[i+1][j][0],f[i][j][1]→ ...
- Luogu4926 倍杀测量者(二分答案+差分约束)
容易想到二分答案.问题变为判断是否所有条件都被满足,可以发现这是很多变量间的相对关系,取个log之后就是经典的差分约束模型了.特殊的地方在于某些人的分数已被给定,从每个人开始跑一遍最短路判断一下是否能 ...
- P1107 [BJWC2008]雷涛的小猫
题目描述 雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样的行为是违反学生宿舍管理条例的).在他的照顾下,小猫很快恢复了健康,并且愈发的活泼可爱了. 可是有一天,雷涛下课回 ...
- P2573 [SCOI2012]滑雪
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着 M 条供滑行的轨道和 N 个轨道之间的交点(同时也是景点),而且每个景点都有一编号 i ( 1≤i≤N )和一高度 Hi.a18028 ...
- Python清理过期文件
改程序执行后,会清理 test/文件夹中距离现在超过一天的以 .xml 结尾的文件 # coding: utf-8 import time import os root = os.path.dirna ...
- EVE-NG硬盘扩容,存储海量镜像
EVE-NG硬盘扩容,存储海量镜像 来源 http://blog.51cto.com/sms1107/1928453 一.查看当前磁盘使用情况 /dev/mapper/eve--ng--vg-root ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...