zoj 3940E

来源:互联网 发布:毛衣品牌 知乎 编辑:程序博客网 时间:2024/06/05 00:07

题目链接

E - Modulo Query

分析

不要问我为什么,看了题解再yy一下你就懂了.
题解

AC code

#include <cstdio>#include <iostream>#include <vector>#include <queue>#include <algorithm>#include<cmath>#include <cstring>#include <map>#include <iomanip>#define fi first#define se second#define INF 0x3f3f3f3f3f3f3f3fusing namespace std;const int MOD = 1e9+7;const int MAX_P = 2e4+10;const int maxn =2e5+10;const int MAX_V = 5e5+10;const int maxv = 1e6+10;typedef long long LL;typedef long double DB;typedef pair<LL,LL> Pair;LL sum[maxn];Pair interval[maxn];int main(int argc, char const *argv[]) {    int T;    cin>>T;    while (T--) {        int n,m;        cin>>n>>m;        priority_queue<Pair ,std::vector<Pair> ,less<Pair> > Q;        Q.push(Pair(m,1));        while (n--) {            int s;            scanf("%d",&s );            while (Q.top().fi >= s) {                Pair tmp = Q.top();Q.pop();                while (!Q.empty() && Q.top().fi == tmp.fi){                    tmp.se+=Q.top().se;Q.pop();                }                Q.push(Pair(s-1,(tmp.fi+1)/s*tmp.se));//产生在(s-1)内的数的方法总数.                if((tmp.fi+1) % s){                    Q.push(Pair(tmp.fi % s,tmp.se));                }            }        }        int cnt =0;        while (!Q.empty()) {            Pair tmp = Q.top();Q.pop();                while (!Q.empty() &&tmp.fi == Q.top().fi ) {                    tmp.se+=Q.top().se;                    Q.pop();                }            interval[cnt++] = tmp;        }        //std::cout << "cnt " << cnt << '\n';        sort(interval,interval+cnt);        sum[0] = interval[0].se;        //std::cout << "sum" << '\n';        for(int i=1 ; i<cnt ; ++i){             sum[i] = sum[i-1]+interval[i].se;             //std::cout << sum[i] << '\n';        }        int q;        cin>>q;        int i=0;        LL ans =0;        while (q--) {            i++;            int y;            scanf("%d", &y);            int pos = lower_bound(interval,interval+cnt,Pair(y,0))-interval;            LL tmp =0;            if(pos < cnt)tmp = sum[cnt-1]-sum[pos-1];            ans = (ans+tmp*i) % MOD;        }        std::cout << ans << '\n';    }    return 0;}
0 0
原创粉丝点击