标签:
题目链接:
Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar()); for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar()); F && (num=-num); } int stk[70], tp; template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + ‘0‘); putchar(‘\n‘); } const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=1e9; const int N=1e5+10; const int maxn=500+10; const double eps=1e-10; int a[N],n,d[N][20]; map<int,LL>mp; int gcd(int x,int y) { if(y==0)return x; return gcd(y,x%y); } inline void rmq() { for(int i=1;i<=n;i++)d[i][0]=a[i]; for(int j=1;(1<<j)<=n;j++) { for(int i=1;i+(1<<j)-1<=n;i++) { d[i][j]=gcd(d[i][j-1],d[i+(1<<(j-1))][j-1]); } } return ; } inline int query(int l,int r) { int k=0; while((1<<(k+1))<=r-l+1)k++; return gcd(d[l][k],d[r-(1<<k)+1][k]); } inline void Init() { mp.clear(); For(i,1,n) { int pos=i; while(pos<=n) { int le=query(i,pos); int l=pos,r=n; while(l<=r) { int mid=(l+r)>>1; if(query(i,mid)<le)r=mid-1; else l=mid+1; } mp[le]+=(LL)(r-pos+1); pos=r+1; } } } int main() { int t,Case=0; read(t); while(t--) { printf("Case #%d:\n",++Case); read(n); For(i,1,n)read(a[i]); rmq(); Init(); int q; read(q); int l,r; while(q--) { read(l);read(r); int ans=query(l,r); printf("%d ",ans); print(mp[ans]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5699148.html