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− ...
随机推荐
- 结对编程学习fault、error、failure三种状态
点滴成就 学习时间 新编写代码行数 博客量(篇) 学习知识点 第一周 10小时 0 0 了解软件工程 第二周 10小时 0 1 项目开题 第三周 15小时 0 1 开通博客.开展项目调查 第四周 20 ...
- 关于java读取excle文件的相关方法 ;
1.读取Excle文件内容的方法 拿过来可以直接用 : 2.参照 http://www.anyrt.com/blog/list/importexcel.html#6 更多知识请参考:http://ww ...
- js 实现路由功能
class Router { constructor() { this.routes = [] } handle(pattern, handler) { this.routes.push({ patt ...
- python判断字符串是否包含子字符串
python的string对象没有contains方法,不可以使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数 python的st ...
- post方法的数据类型
form-data.x-www-form-urlencoded.raw.binary的区别 1. form-data 就是http请求中的multipart/form-data,它会将表单的数据处理为 ...
- scoket常用函数简单介绍
scoket: 是一种抽象层,应用程序通过它来发送和接收数据,使用socket可以将应用程序添加到网络中,与处于同一网络中的其他应用程序进行通信. 简单来说,scoket提供了程序内部与外界通道的 ...
- 【Mybatis】<foreach>标签在mybatis中的使用
mapper.xml如下: <select id="selectCkspcb" parameterType="java.util.Map" resultT ...
- Python常忘的基础知识
0.目录 1.进制 1.1 各进制的表示 1.2 各进制的转换 2.字符 2.1 转义字符 2.2 原始字符串 3.类型 3.1 基本数据类型 3.2 type() 4.变量与运算符 4.1 值类型与 ...
- P2169 正则表达式
题目背景 小Z童鞋一日意外的看到小X写了一个正则表达式的高级程序,这个正则表达式程序仅仅由字符“0”,“1”,“.”和“*”构成,但是他能够匹配出所有在OJ上都AC的程序的核心代码!小Z大为颇感好奇, ...
- windows7下的64位redis安装简介
在网上找了好多,指向的都是同一个地址,可惜打不开.https://github.com/MSOpenTech/redis/releases.网址被禁掉了.终于找到一篇有用的帖子,安装成功.感谢仁兄ko ...