Bayan 2015 Contest Warm Up D题:区间gcd为定值对数:循环思维技巧(pair+map)

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<map>
 4 #include<algorithm>
 5 using namespace std;
 6 map<int,long long>ans;
 7 pair<int,long long>mp1[100005];
 8 pair<int,long long>mp2[100005];
 9 int a[100005];
10 int gcd(int x,int y)
11 {
12     if (y==0) return x;
13     return gcd(y,x%y);
14 }
15 int main()
16 {
17     int n,i,j,pre=0,now,q,temp;
18     scanf("%d",&n);
19     for (i=1;i<=n;i++) scanf("%d",&a[i]);
20     for (i=1;i<=n;i++)
21     {
22         now=1;
23         mp2[now]=make_pair(a[i],1);
24         for (j=1;j<=pre;j++)
25         {
26             temp=gcd(mp1[j].first,a[i]);
27             if (mp2[now].first==temp) mp2[now].second+=mp1[j].second;
28             else mp2[++now]=make_pair(temp,mp1[j].second);
29         }
30         pre=now;
31         for (j=1;j<=now;j++) {
32             mp1[j]=mp2[j];
33             ans[mp1[j].first]+=mp1[j].second;
34         }
35     }
36     scanf("%d",&q);
37     while (q--)
38     {
39         scanf("%d",&j);
40         printf("%I64d\n",ans[j]);
41     }
42 }

http://codeforces.com/contest/475/problem/D

posted on 2014-10-08 20:55  xiao_xin  阅读(81)  评论(0编辑  收藏  举报

导航