codeforces 1362 E 进制的性质
写法想多,对结果无优化的方法不必要多写。
最开始思路是想把k数组的偶数个相同的消除,
6 2
1 2 3 3 3 4
即变成1 2 3 4,然后从大到小排序,因为
然后用最大的那个数pow减去后面的即可。
但是徒增操作量,因为倘若n个数都不相同,则时间复杂度不变。
所以可以直接从大到小排序,然后直接计算即可。
还有一个细节
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll mod = 1e9+7; ll MOD = 1e9+3; ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 1e6+10; bool cmp(ll a,ll b) { return a > b; } ll k[maxn]; int main() { //freopen("input.txt", "r", stdin); int _; scanf("%d",&_); while(_--) { ll n,p; cin>>n>>p; rep(i,1,n) cin>>k[i]; if(p==1){printf("%d\n",n%2);continue;} sort(k+1,k+1+n,cmp); ll ans = 0,res = 0; rep(i,1,n) { if(ans == 0 && res == 0) { ans = powmod(p,k[i],mod); res = powmod(p,k[i],MOD); } else { ans = (ans - powmod(p,k[i],mod) + mod)%mod; res = (res - powmod(p,k[i],MOD) + MOD)%MOD; } } cout<<ans<<endl; } }