codeforces E. Johnny and Grandmaster

在這裏插入圖片描述

題目

題意:

給你n,p,kin,p,k_i問把pkip^{k_i}分成兩部分,問兩部分的每部分的和的差的絕對值最小是多少。

思路:

我們可以發現,pkip^{k_i}是次方的關係,我們從大到小的排序後,我們可以發現,如果存在ii後面的數字總和大於pkip^{k_i}的話,那麼肯定有元素和是等於pkip^{k_i}的,因爲xxi=xi+1x * x ^ i=x ^ {i+1},那麼我們是不是每次都可以用一個最大的,然後直到出現pki=pki+1+pki+2+......p^{k_i}=p^{k_{i+1}}+p^{k_{i+2}}+......,這樣就分成兩部分,並且絕對值爲00,直到最後不出現後出現的差值就是最終答案了,但是我們需要注意的一點就是,這個數字很大,需要取模,那麼如果只取模一個數字的話,如果剛好是modmod的倍數的話,那麼就會出現錯誤,所以還需要再隨意取模一個數字。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    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 ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int modn = 1e8 + 6;
ll k[maxn];
ll quickpow(ll a, ll b, ll mod) {
    ll ans = 1, base = a;
    while (b) {
        if (b & 1) ans = ans * base % mod;
        base = base * base % mod;
        b >>= 1;
    }
    return ans;
}
bool cmp(ll a, ll b) {return a > b;}
int main() {
    int t;
    read(t);
    while (t--) {
        ll n, p, x;
        read(n), read(p);
        for (int i = 1; i <= n; i++) read(k[i]);
        ll ans = 0, ansn = 0;
        sort(k + 1, k + n + 1, cmp);
        for (int i = 1; i <= n; i++) {
            if (ans == 0 && ansn == 0) ans += quickpow(p, k[i], mod), ansn += quickpow(p, k[i], modn);
            else {
                ans = (ans - quickpow(p, k[i], mod) + mod) % mod;
                ansn = (ansn - quickpow(p, k[i], modn) + modn) % modn;
            }
        }
        outl(ans);
        putchar('\n');
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章