Problem B. Sherlock and Watson Gym Secrets
Watson and Sherlock are gym buddies.
Their gym trainer has given them three numbers, A, B, and N, and has asked Watson and Sherlock to pick two different positive integers i and j, where i and j are both less than or equal to N. Watson is expected to eat exactly iA sprouts every day, and Sherlock is expected to eat exactly jB sprouts every day.
Watson and Sherlock have noticed that if the total number of sprouts eaten by them on a given day is divisible by a certain integer K, then they get along well that day.
So, Watson and Sherlock need your help to determine how many such pairs of (i, j) exist, where i != j. As the number of pairs can be really high, please output it modulo 109+7 (1000000007).
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of one line with 4 integers A, B, N and K, as described above.
Output
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the required answer.
Limits
1 ≤ T ≤ 100.
0 ≤ A ≤ 106.
0 ≤ B ≤ 106.
Small dataset
1 ≤ K ≤ 10000.
1 ≤ N ≤ 1000.
Large dataset
1 ≤ K ≤ 100000.
1 ≤ N ≤ 1018.
Sample
In Case 1, the possible pairs are (1, 2), (1, 5), (2, 1), (2, 4), (4, 2), (4, 5), (5, 1), and (5, 4).In Case 2, the possible pairs are (1, 2), (1, 3), and (4, 1).
In Case 3, No possible pairs are there, as i != j.
Solution
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <list>
#include <iterator>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long ll;
const int MAXK = 1e5;
ll i_A[MAXK+1], j_B[MAXK+1], i_j_same[MAXK+1];
int pow_mod(int a, int n, int mod){
int r = 1 % mod;
while (n) {
if (n&1)
r = (ll)r*a%mod;
a = (ll)a*a%mod;
n >>= 1;
}
return r;
}
int main(int argc, char* argv[])
{
#ifndef TEST
if (argc != 2)
{
cout << "Invalid input" << endl;
return 1;
}
string input = argv[1];
string output = input.substr(0, input.length() - 2) + "out";
freopen(input.c_str(), "r", stdin);
freopen(output.c_str(), "w", stdout);
#endif
const ll MOD = 1e9 + 7;
int T;
int A, B;
ll N, K;
cin >> T;
for (int i = 1; i <= T; i++)
{
memset(i_A, 0, sizeof(i_A));
memset(j_B, 0, sizeof(j_B));
memset(i_j_same, 0, sizeof(i_j_same));
cin >> A >> B >> N >> K;
ll res = 0;
for (int p = 1; p <= min(N, K); p++)
{
ll cnt = ((N - p) / K + 1) % MOD;
int tmp_A = pow_mod(p, A, K), tmp_B = pow_mod(p, B, K);
i_A[tmp_A] += cnt;
j_B[tmp_B] += cnt;
if ((tmp_A + tmp_B) % K == 0) // (0 + 0) != K, but (0 + 0) % K == 0
i_j_same[p%K] += cnt;
}
for (int p = 0; p < K; p++)
{
int q = (K - p) % K; // Convert (0, K) to (0, 0)
res += ((i_A[p] % MOD) * (j_B[q] % MOD)) % MOD;
res -= i_j_same[p];
res %= MOD;
}
res += MOD;
res %= MOD;
cout << "Case #" << i << ": " << res << endl;
}
fclose(stdin);
fclose(stdout);
return 0;
}
Note
首先要会计算Fast Modular Exponentiation,见我另一篇博客http://blog.csdn.net/ywcpig/article/details/52566496。
其次,要知道数论中同余的基本性质(同加、同乘)。
本题答案 = 所有(i^A+j^B)%K==0的情况数量 - i==j的特例数量
注意到:((i+K)^A)%K == (i^A)%K
所以,只需要考虑i=1到K的情况就行。
综上,本题解法的时间复杂度为O(KlogK)
Reference
https://code.google.com/codejam/contest/5254487/dashboard#s=p1