【HDU5726 2016 Multi-University Training Contest 1D】【gcd的下降性质 STL-map】GCD 多少段区间gcd等于给定区间gcd
2016-07-25 09:49
477 查看
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2189 Accepted Submission(s): 731
[align=left]Problem Description[/align]
Give you a sequence of N(N≤100,000) integers
: a1,...,an(0<ai≤1000,000,000).
There are Q(Q≤100,000) queries.
For each query l,r you
have to calculate gcd(al,,al+1,...,ar) and
count the number of pairs(l′,r′)(1≤l<r≤N)such
that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).
[align=left]Input[/align]
The first line of input contains a number T,
which stands for the number of test cases you need to solve.
The first line of each case contains a number N,
denoting the number of integers.
The second line contains N integers, a1,...,an(0<ai≤1000,000,000).
The third line contains a number Q,
denoting the number of queries.
For the next Q lines,
i-th line contains two number , stand for the li,ri,
stand for the i-th queries.
[align=left]Output[/align]
For each case, you need to output “Case #:t” at the beginning.(with quotes, t means
the number of the test case, begin from 1).
For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and
the second number stands for the number of pairs(l′,r′) such
that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).
[align=left]Sample Input[/align]
1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
[align=left]Sample Output[/align]
Case #1:
1 8
2 4
2 4
6 1
[align=left]Author[/align]
HIT
[align=left]Source[/align]
2016 Multi-University Training
Contest 1
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, v
;
vector< pair<int,int> >lft
;
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x%y);
}
map<int, LL>mop;
void STinit()
{
lft[n + 1].clear();
for (int i = n; i >= 1; i--)
{
lft[i].clear();
int p = i; int g = v[i];
for (auto& it : lft[i + 1])
{
int newg = gcd(g, it.second);
if (newg != g)lft[i].push_back(MP(p, g));
p = it.first;
g = newg;
}
lft[i].push_back(MP(p, g));
p = i - 1;
for (auto& it : lft[i])
{
mop[it.second] += it.first - p;
p = it.first;
}
}
}
int getgcd(int l, int r)
{
for (auto& it : lft[l])
{
if (r <= it.first)return it.second;
}
}
int main()
{
scanf("%d", &casenum);
for (casei = 1; casei <= casenum; ++casei)
{
mop.clear();
scanf("%d", &n); for (int i = 1; i <= n; ++i)scanf("%d", &v[i]);
STinit();
int q; scanf("%d", &q);
printf("Case #%d:\n", casei);
while (q--)
{
int l, r; scanf("%d%d", &l, &r);
int g = getgcd(l, r);
printf("%d %lld\n", g, mop[g]);
}
}
return 0;
}
/*
【trick&&吐槽】
比赛的时候把最后一个区间段忘记加进去了,导致吃了一发WA,悲剧!
【题意】
有n(1e5)个数,
每次给你一个区间段,
问你有多少个区间段内的gcd与该区间段的gcd是相同的,输出这样的区间个数。
【类型】
gcd的下降性质
【分析】
我们以一个点为区间的左界,然后逐渐扩展右界,gcd一旦变化了,至少也会变为原来的一半。
所以gcd的变化区间段数最多为logn段。
于是我们可以以区间的右侧开始,向右方向扩展gcd,然后存到map中。
这个预处理的复杂度是O(nlogn)
接下来对于单组数据的查询和询问,复杂度也是O(nlogn),这道题就做完了。
【时间复杂度&&优化】
O(nlogn)
*/
相关文章推荐
- 【Codeforces Round 323 (Div 2)C】【观察找规律 STL map】GCD Table 从GCD矩阵中找出所有原始元素
- 【HDU5565 BestCoder Round 62 (div1)C】【STL or 二分答案 or 计数排序】Clarke and baton n个人减肥m次求最后异或值
- 【HDU5564 BestCoder Round 62 (div1)B】【DP转矩阵快速幂】Clarke and digits 长度在[l,r]范围内7倍数数个数要求相邻两位不为K
- 【HDU5544 2015CCPC 南阳国赛E】【树上dfs找本质不同环 高斯消元 时间戳优化】Ba Gua Zhen 连通图上最大异或环
- 【HDU5222 2015赛码冠军杯I】【并查集找双连通 + tarjan求强连通】Exploration 双向边只能走一边是否图上存在环
- 【HDU5188 BestCoder Round 33C】【贪心排序+DP】zhx and contest 考试不被怀疑作弊条件下达到至少m分的最少时间
- 【HDU4560 2013西山居复赛D】【二分答案+网络流拆点】我是歌手 安排演唱会_每人歌不同_每场歌不同_人歌匹配一次
- 【HDU5411 2015 Multi-University Training Contest 10F】【矩阵快速幂 加一行构造法】CRB and Puzzle 矩阵的1次方到n次方的数值和
- 【HDU5570 BestCoder Round 63 (div1)C】【期望DP 公式化简】balls n种求m种颜色,同颜色球数为x贡献为x方 求期望
- 【HDU5569 BestCoder Round 63 (div1)B】【DP】matrix 向右走向下走最大乘积和
- 【HDU5568 BestCoder Round 63 (div1)A】【DP java高精度】sequence2 长度恰好为m的LIS数
- 【HDU5579 2015上海赛区G】【超级大讨论】Game of Arrays a[]+b[]+c[]有些位置可以减一,状态是否可能达成
- 【HDU5583 2015上海赛区L】【找规律 正难则反】LCM Walk 目标状态(x,y)哪些点走公倍数能走到它
- 【HDU5573 2015上海赛区B】【构造 二进制思想】Binary Tree 二叉树上走m层加减数使得最后权值恰为n
- 【HDU5583 2015上海赛区K】【暴力合并】Kingdom of Black and White 连续01串权值贡献为len^2最多改变一次最大权值
- 【HDU5578 2015上海赛区F】【水题】Friendship of Frog 最近的两个相同字符的距离是多少
- 【HDU5586 BestCoder Round 64 (div1)A】【贪心 最大连续子串】Sum 区间函数值变换使得数列权值和最大
- 【HDU5587 BestCoder Round 64 (div1)B】【迭代 前缀和思想】Array 前m个数的二进制中共有多少个1
- 【Educational Codeforces Round 2B】【map or 二分查找】Queries about less or equal elements b[]中的每个数比a[]中多少数大
- 【Educational Codeforces Round 2E】【STL-map 启发式合并 or 线段树动态开节点 】Lomsat gelral 一棵树每点一个颜色问每个节点子树的颜色众数之和