# HDU 5726 GCD(列舉暴力)

http://acm.hdu.edu.cn/showproblem.php?pid=5726

``````#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   MAX           100005
#define   MAXN          6005
#define   maxnode       15
#define   sigma_size    30
#define   lson          l,m,rt<<1
#define   rson          m 1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000
//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
//const double inf   = 1e18;
const double eps   = 1e-8;
const LL    mod    = 998244353LL;
const ull    mx    = 133333331;
/*****************************************************/
inline void RI(int &x) {
char c;
while((c=getchar())<'0' || c>'9');
x=c-'0';
while((c=getchar())>='0' && c<='9') x=(x<<3) (x<<1) c-'0';
}
/*****************************************************/
map<int,LL> d[2],ans;
int a[MAX];
int g[MAX<<2];
int gcd(int a,int b){
if(!b) return a;
return gcd(b,a%b);
}
void pushup(int rt){
g[rt]=gcd(g[lrt],g[rrt]);
}
void build(int l,int r,int rt){
if(l==r){
g[rt]=a[l];
return ;
}
middle;
build(lson);
build(rson);
pushup(rt);
}
int query(int l,int r,int rt,int L,int R){
if(L<=l&&r<=R) return g[rt];
middle;
int ans=0;
if(L<=m) ans=gcd(ans,query(lson,L,R));
if(R>m) ans=gcd(ans,query(rson,L,R));
return ans;
}
int main(){
int t,kase=0;
cin>>t;
while(t--){
int n;
cin>>n;
kase  ;
ans.clear();
d[0].clear();
d[1].clear();
for(int i=1;i<=n;i  ) scanf("%d",&a[i]);
build(1,n,1);
int cnt=0,tmp=1;
d[cnt][a[1]]  ;
ans[a[1]]  ;
for(int i=2;i<=n;i  ){
d[tmp][a[i]]  ;
ans[a[i]]  ;
for(map<int,LL>::iterator it=d[cnt].begin();it!=d[cnt].end();it  ){
int x=gcd(a[i],it->first);
d[tmp][x] =it->second;
ans[x] =it->second;
}
d[cnt].clear();
swap(cnt,tmp);
}
int m;
cin>>m;
printf("Case #%d:\n",kase);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
int x=query(1,n,1,a,b);
printf("%d %I64d\n",x,ans[x]);
}
}
return 0;
}``````