-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_p^ki's_in_2_grps.cpp
100 lines (98 loc) · 2.47 KB
/
divide_p^ki's_in_2_grps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define fill(a,val) memset(a,val,sizeof(a))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
const ll MOD = 1000*1000*1000+7;
const ll INF = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2 = 998244353;
const ld PI = 3.141592653589793;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll power1(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);}return res;}
ll power2(ll x,ll y,ll n){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=(x*x);if(res>n)return -1;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}
int max(int a,int b){if(a>b)return a;return b;}
int min(int a,int b){if(a>b)return b;return a;}
bool sortbysec(const pair<string,ll> &a, const pair<string,ll> &b)
{
return a.second<b.second;
}
bool check(ll diff,ll p,ll* v,ll n)
{
if(power2(p,diff,n)==-1 || power1(p,diff)*(*v)>n)
{
return false;
}
else
{
*v=(*v)*power1(p,diff)-1;
}
return true;
}
int main(void){
fastio();
ll t;
cin>>t;
while(t--)
{
ll n,p;
cin>>n>>p;
ll arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
ll v=1;
bool d[n];
d[n-1]=true;
ll flag=0;
ll diff;
for(int i=n-2;i>=0;i--)
{
if(flag==1)
{
d[i]=false;
continue;
}
if(v==0)
{
d[i]=true;
v=1;
}
else
{
diff=arr[i+1]-arr[i];
// cout<<diff<<" "<<v<<"\n";
bool var=check(diff,p,&v,n);
d[i]=false;
if(var==false)
{
flag=1;
}
}
// cout<<v<<"\n";
}
ll ans=0;
for(int i=n-1;i>=0;i--)
{
if(d[i]==true)
{
ans=(ans+power(p,arr[i],MOD))%MOD;
}
else
{
ans=(ans-power(p,arr[i],MOD))%MOD;
if(ans<0)ans+=MOD;
}
}
cout<<ans<<"\n";
}
}