HDU5726 GCD
Posted bobhuang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5726 GCD相关的知识,希望对你有一定的参考价值。
InputThe first line of input contains a number TT, which stands for the number of test cases you need to solve.
The first line of each case contains a number NN, denoting the number of integers.
The second line contains NN integers, a1,...,an(0<ai≤1000,000,000)a1,...,an(0<ai≤1000,000,000).
The third line contains a number QQ, denoting the number of queries.
For the next QQ lines, i-th line contains two number , stand for the li,rili,ri, stand for the i-th queries.
OutputFor each case, you need to output “Case #:t” at the beginning.(with quotes, tt 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)gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l′,r′)(l′,r′) such that gcd(al′,al′+1,...,ar′)gcd(al′,al′+1,...,ar′)equal gcd(al,al+1,...,ar)gcd(al,al+1,...,ar).
Sample Input
1 5 1 2 4 6 7 4 1 5 2 4 3 4 4 4
Sample Output
Case #1: 1 8 2 4 2 4 6 1
我的就是区间取值+暴力查询,因为gcd很少。他们做法是二分+RMQ,我还是觉得我的做法简单
#include<bits/stdc++.h> using namespace std; int fun(int x,int y) { return __gcd(x,y); } #define fi first #define se second const int N=1e5+5; int n,a[N],l[N],v[N]; vector<pair<pair<int,int>,int> >ans[N]; unordered_map<int,long long>M; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T,ca=0; cin>>T; while(T--) { cin>>n; for(int i=1; i<=n; i++)cin>>a[i]; for(int i=1,j; i<=n; i++) for(v[i]=a[i],j=l[i]=i; j; j=l[j]-1) { v[j]=fun(v[j],a[i]); while(l[j]>1&&fun(a[i],v[l[j]-1])==fun(a[i],v[j]))l[j]=l[l[j]-1]; M[v[j]]+=j-l[j]+1; ans[i].push_back(make_pair(make_pair(j,l[j]),v[j])); } int q; cin>>q; cout<<"Case #"<<++ca<<": "; for(int i=0,l,r,x;i<q;i++) { cin>>l>>r; for(auto X:ans[r]) { if(X.fi.fi>=l&&X.fi.se<=l) { x=X.se; break; } } cout<<x<<" "<<M[x]<<" "; } M.clear(); for(int i=1;i<=n;i++)ans[i].clear(); } return 0; }
以上是关于HDU5726 GCD的主要内容,如果未能解决你的问题,请参考以下文章
2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)