zoj 3940E - Modulo Query (数学)

题目链接

E - Modulo Query

分析

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

AC code

#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#define fi first
#define se second
#define INF 0x3f3f3f3f3f3f3f3f
using 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 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_queuestd::vector ,less > 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 ; i1]+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;
}

你可能感兴趣的:(算法&数据结构)