Please, try EDU on Codeforces! New educational section with videos, subtitles, texts, and problems. ×

saurabhkumarfx's blog

By saurabhkumarfx, history, 4 weeks ago, In English,
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
const int MOD = 1e9 + 7 , mod = 999998639;
int k[N] , n , p;
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
signed main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t --)
    {
        cin >> n >> p;
        for(int i = 1 ; i <= n ; i ++) cin >> k[i];
        sort(k + 1 , k + 1 + n , greater<int>());
        int ans1 = 0 , ans2 = 0;
        for(int i = 1 ; i <= n ; i ++)
        {
            if(!ans1 && !ans2) 
                ans1 += pow_mod(p , k[i] , MOD), 
                ans2 += pow_mod(p , k[i] , mod);
            else
                ans1 = (ans1 - pow_mod(p , k[i] , MOD) + MOD) % MOD,
                ans2 = (ans2 - pow_mod(p , k[i] , mod) + mod) % mod;
        }
        cout << ans1 << '\n';
    }
    return 0;
}

Can somebody explain why this solution works for last (DIV2E).It really feels easy to code but what is logic behind another mod ?? https://codeforces.com/contest/1361/problem/B

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it