【題解】Codeforces 1329B. Dreamoon Likes Sequences

【題目敘述】http://codeforces.com/contest/1329/problem/B

#include <iostream>
#include <vector>
using namespace std;
 
int t, d, m;
long long ans;
 
int main() {
    cin >> t;
    while (t--){
        cin >> d >> m;
        ans = 1;
        int cur = 1;
        while (d){
            ans *= min(cur, d)+1;
            ans %= m;
            d -= min(cur, d);
            cur *= 2;
        }
        ans -= 1;
        if (ans < 0) ans += m;
        cout << ans << "\n";
    }
}

分享本文 Share with friends