Round B APAC Test 2017 Problem B. Sherlock and Watson Gym Secrets
来源:互联网 发布:appcan怎么调用数据库 编辑:程序博客网 时间:2024/05/22 06:08
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
- Round B APAC Test 2017 Problem B. Sherlock and Watson Gym Secrets
- Problem B. Sherlock and Watson Gym Secrets Google APAC 2017 University Test Round B
- Round B APAC Test 2017 Problem A. Sherlock and Parentheses
- Round B APAC Test 2017 Problem C. Watson and Intervals
- Round B APAC Test 2017 Problem D. Sherlock and Permutation Sorting
- Problem A. Sherlock and Parentheses Google APAC 2017 University Test Round B
- Problem C. Watson and Intervals Google APAC 2017 University Test Round B
- Round C APAC Test 2017 Problem B. Monster Path
- Practice Round APAC test 2017--Problem B. Robot Rock Band
- Round E APAC Test 2017 Problem B. Beautiful Numbers
- Problem B. Rain Google APAC 2017 University Test Round A
- Round C APAC Test 2017 Problem B. Safe Squares (C++)
- Round A APAC Test 2017 Problem B.Rain
- Problem B. gWheels Google APAC 2016 University Test Round B
- Google APAC 2017 University Test Round B
- Round B APAC Test 2016
- Google APAC test 2015 Round B Problem A - Password Attacker
- Google APAC test 2015 Round B Problem C - Card Game
- 事务四大特征:原子性,一致性,隔离性和持久性(ACID)
- iOS开发之图形渲染分析、离屏渲染、当前屏幕渲染、On-Screen Rendering、Off-Screen Rendering
- 第4课矩阵的LU分解
- 内存对齐
- Android studio导出jar包
- Round B APAC Test 2017 Problem B. Sherlock and Watson Gym Secrets
- web前端开发小结
- 第5课 转置,置换,向量空间R
- 高仿微信和qq气泡聊天(整合精简版)
- WebView 无法执行js代码
- mahout 推荐系统
- 【C++】输入多行字符,含空格
- YII2.0探究二:AR类和Query类的原理以及使用(一)
- nodejs调试之使用node-inspector