HDU 5726-GCD(暴力+map)
来源:互联网 发布:2016cpa电影网站源码 编辑:程序博客网 时间:2024/06/04 17:44
GCD
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2527 Accepted Submission(s): 892
Problem Description
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) .
Input
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 numberN , denoting the number of integers.
The second line containsN integers, a1,...,an(0<ai≤1000,000,000) .
The third line contains a numberQ , denoting the number of queries.
For the nextQ lines, i-th line contains two number , stand for the li,ri , stand for the i-th queries.
The first line of each case contains a number
The second line contains
The third line contains a number
For the next
Output
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 forgcd(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) .
For each query, you need to output the two numbers in a line. The first number stands for
Sample Input
151 2 4 6 741 52 43 44 4
Sample Output
Case #1:1 82 42 46 1
Author
HIT
Source
2016 Multi-University Training Contest 1
Recommend
wange2014 | We have carefully selected several similar problems for you: 5808 5807 5806 5805 5804
题目意思:
有一串N个数字,Q个查询给出一组区间,求区间内各个数的GCD以及共有多少个区间的书的GCD与给定区间的GCD相同。
解题思路:
下面的代码是标程,根据题解说说自己的理解。
每输入一个数a[i]就进行一次处理:依次计算以a为结尾的子序列的GCD,就是说a所在的位置是区间的最右端点,
定义vector<pair<int,int> > g[maxn];其中first:gcd;second:区间左端点。
即first=GCD(a[second],a[i]),g[i][j]中i表示输入的第i个数,j表示当前处理第i个数时保存的第j组不同GCD。
定义map<ll,ll>m;m[i]=j,表示GCD为i的区间共有j个。
所以保存记录map的时候,m[g[i][j].first]+=(j==g[i].size()-1?i+1:g[i][j+1].second)-g[i][j].second;
对单个的a[i]GCD为本身进行了特判,其它的就是利用下一个的区间左端减去当前区间左端的差值作为当前新增的GCD个数。
扫map之前找GCD的查询的时,固定右端点,查询左端点。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <map>#include <vector>#include <cstdlib>#define maxn 100010#define ll long longusing namespace std;map<ll,ll>m;vector<pair<int,int> > g[maxn];//first:gcd;second:区间左端点int gcd(int a,int b)//求ab两数的最大公约数{ return b?gcd(b,a%b):a;}ll a[maxn];int main(){ ios::sync_with_stdio(false); cin.tie(0); int t,ca=0; cin>>t; while(t--) { m.clear(); cout<<"Case #"<<++ca<<":"<<endl; int n,q,l,r,i,j; cin>>n; for(int i=0; i<=n; ++i) g[i].clear();//不清空会超内存 //预处理出所有的以L开头的左区间的gcd(al,al+1,...,ar): //对于每个数字,记录从开始到当前数字的区间的子区间中以当前数字为结尾区间的gcd //相同gcd区间合并 for(i=1; i<=n; ++i) { int last=0; cin>>a[i]; for(j=0; j<g[i-1].size(); ++j) { int tmp=gcd(a[i],g[i-1][j].first); if(tmp==last) continue;//上一个出现过该gcd last=tmp; g[i].push_back(make_pair(tmp,g[i-1][j].second)); } if(last!=a[i])//当前a与自己的gcd在上面的循环中没有出现过 g[i].push_back(make_pair(a[i],i)); //用一个map来记录,gcd值为key的有多少个 for(j=0; j<g[i].size(); ++j) { m[g[i][j].first]+=(j==g[i].size()-1?i+1:g[i][j+1].second)-g[i][j].second; // cout<<g[i][j].first<<" "<<m[g[i][j].first]<<endl; } } cin>>q; for(i=0; i<q; ++i) { cin>>l>>r; for(j=0; j<g[r].size(); ++j)//对于每个询问只需要查询对应gcd(al,al+1,...,ar)为多少 if(g[r][j].second>l) break; cout<<g[r][j-1].first<<" "<<m[g[r][j-1].first]<<endl; } } return 0;}/**151 2 4 6 741 52 43 44 4**/
0 0
- HDU 5726-GCD(暴力+map)
- HDU 5726 GCD(枚举暴力)
- HDU 5019 Revenge of GCD (暴力)
- HDU 4858 Shaolin(map暴力)
- HDU 5908 Abelian Period (暴力+map)
- hdu 5908 暴力(map)
- HDU 5726 GCD 区间gcd查询 MAP RMQ 优化
- HDU 5726 GCD [ST表+暴力二分]【数据结构|杂类】
- hdu 5726 区间gcd RMQ+二分 || 暴力枚举
- HDU 5726 GCD 【GCD】【ST表+二分】【线段树+暴力枚举】
- HDU 2504 又见GCD【数学+暴力】
- 【HDU】5930 GCD【暴力+线段树二分】
- 文章标题 HDU 5578 :Friendship of Frog(暴力+map)
- HDU 2586 How far away ?(map+lca【暴力水】)
- (HDU 5726)GCD <RMQ + map + 二分> 多校训练1
- HDU 5726--GCD【倍增】【单调栈】【STL-map】
- hdu 5908 Abelian Period(暴力 + map优化)
- HDU -- 5908 Abelian Period 【思维 + map + 暴力】
- Inkscape tutorial: Shapes
- MyBatis基础
- hdu1232 畅通工程--并查集
- hdu1879 继续畅通工程--prim
- Atom 插件推荐
- HDU 5726-GCD(暴力+map)
- 写一个函数替代strrev()--实现字符串翻转
- Qt5开发
- setInterval设置停止和循环
- 五种方式获取一个文件的扩展名
- 日常总结_2(补八月七日)
- 暑假留校--第二周总结
- php 首字母及"_"后首字母都要大写(实现字符串中"open_door"转换成"OpenDoor")
- django与ajax