codeforces475D——CGCDSSQ

来源:互联网 发布:网络医院预约挂号系统 编辑:程序博客网 时间:2024/06/05 00:47

1、题意:多次求区间gcd是x的区间数
2、分析:看起来下不了手!管他呢,上暴力,把所有的可能都给他搞出来。每次加入一个数,就把前面的数都和他gcd一下加进去,用map统计数量,最后询问啥输出啥就行。真是可怕,暴力AC了,但是复杂度有保证!可以尝试的证一证。可以证出复杂度是O(nlog2n)

#include <map>#include <set>#include <cmath>#include <queue>#include <vector>#include <bitset>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define M 1000010#define LL long long#define MOD 1000000007#define inf 2147483647#define llinf 4000000000000000000ll#define For(i, x, y) for(int i = (x); i < (y); i ++)#define rep(i, x, y) for(int i = (x); i <= (y); i ++)#define drep(i, x, y) for(int i = (x); i >= (y); i --)inline int read(){    char ch=getchar();int x=0,f=1;    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}inline LL llread(){    char ch=getchar();LL x=0,f=1;    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}map<int, LL> now[2], res;inline int gcd(int x, int y){    return y == 0 ? x : gcd(y, x % y); }int main(){    //freopen("0input.in", "r", stdin);    int n = read(), thi = 0;    rep(i, 1, n){        int x = read();        now[thi ^ 1].clear();         now[thi ^ 1][x] ++; res[x] ++;        for(map<int, LL> :: iterator it = now[thi].begin(); it != now[thi].end(); it ++){            now[thi ^ 1][gcd(x, it -> first)] += it -> second;            res[gcd(x, it -> first)] += it -> second;        }         thi ^= 1;     }    int q = read();    while(q --){        int x = read();        printf("%I64d\n", res[x]);    }    return 0;}
0 0