-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCHFING.cpp
More file actions
57 lines (47 loc) · 1.05 KB
/
CHFING.cpp
File metadata and controls
57 lines (47 loc) · 1.05 KB
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
//https://www.codechef.com/JUNE19B/problems/CHFING
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
ll findModuloExp(ll a, ll b, ll p)
{
ll res = 1;
while(b > 0)
{
if (b & 1)
res = (res*a) % p;
b = b>>1; // b = b/2
a = (a*a) % p;
}
return res;
}
int main()
{
int t ;
cin >> t;
while(t--)
{
ll n, k;
cin >> n >> k;
ll sum = (k-1) % MOD;
if(n >= k)
{
cout<< sum <<endl;
continue ;
}
ll a, d, terms;
a = (k - n) % MOD;
d = -(n-1);
terms = -((k-1)/d);
//sum of AP : (n/2)*(2a + (n-1)d)
ll temp1,temp2 ;
temp1 = (2*a) % MOD ;
temp1 += (d * (terms-1)) % MOD ;
temp1 = (temp1 + MOD) % MOD ;
temp2 = ((terms%MOD) * findModuloExp(2, MOD-2, MOD)) % MOD ;
temp2 = (temp2 * temp1) % MOD ;
sum = (sum + temp2) % MOD;
cout << sum <<endl;
}
return 0;
}