Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

题目链接:点击打开链接


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}
/
const int N = 100000 + 2;
struct Node {
    int pos,l,r;
    ll gval;
    Node(int pos = 0,int l = 0,int r = 0,ll gval = 0):pos(pos),l(l),r(r),gval(gval){}
    bool operator < (const Node & a) const {
        if(gval != a.gval) return gval < a.gval;
        if(pos != a.pos) return pos < a.pos;
        return r < a.r;
    }
};

int n, a[N], tot;
vector<Node> vt[N];
Node node[N * 50];
ll sum[N * 50];
void prepare() {
    for(int i = 0;i <= n;++i) vt[i].clear();
    vt[n].push_back(Node(n,n,n,a[n]));
    Node ntmp;
    int cnt, Size;
    ll x;
    for(int i = n - 1;i >= 1;--i) {
        Size = vt[i + 1].size();
        cnt = 1;
        vt[i].push_back(Node(i,i,i,a[i]));
        for(int j = 0;j < Size;++j) {
            ntmp = vt[i+1][j];
            x = __gcd((ll)a[i],ntmp.gval);
            if(cnt && vt[i][cnt-1].gval == x)
                vt[i][cnt - 1].r = ntmp.r;
            else
                vt[i].push_back(Node(i,ntmp.l,ntmp.r,x)),cnt++;
        }
    }
    tot = 0;
    for(int i = 1;i <= n;++i)
        for(int j = 0;j < (int)vt[i].size();++j)
            node[++tot] = Node(vt[i][j]);
    sort(node + 1,node + tot + 1);
    sum[0] = 0;
    for(int i = 1;i <= tot;++i)
        sum[i] = sum[i-1] + node[i].r - node[i].l + 1;
}
int hehe;
void work(int x) {
    int L, R, l = 0, r = tot + 1, mid;
    while (r - l > 1) {
        mid = (l + r) >> 1;
        if (node[mid].gval > x)
            r = mid;
        else
            l = mid;
    }
    -- r;
    if (r == 0 || node[r].gval != x) {
        putchar('0');
        putchar('\n');
        return ;
    }
    R = r;
    l = 0; r = tot + 1;
    while (r - l > 1) {
        mid = (l + r) >> 1;
        if (node[mid].gval >= x)
            r = mid;
        else
            l = mid;
    }
    L = r;
    pt(sum[R] - sum[L - 1]);
    putchar('\n');
}
int main() {
    rd(n);
    for (int i = 1; i <= n; ++i)
        rd(a[i]);
    prepare();
    int Q, x;
    rd(Q);
    while (Q -- > 0) {
        rd(x);
        work(x);
    }
    return 0;
}


转载于:https://www.cnblogs.com/bhlsheji/p/5167510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值