https://code.google.com/codejam/contest/5254487
A. Sherlock and Parentheses
Problem
Sherlock and Watson have recently enrolled in a computer programming course. Today, the tutor taught them about the balanced parentheses problem. A string S
consisting only of characters (
and/or )
is balanced if:
- It is the empty string, or:
- It has the form
(
S)
, where S is a balanced string, or: - It has the form S1S2, where S1 is a balanced string and S2 is a balanced string.
Sherlock coded up the solution very quickly and started bragging about how good he is, so Watson gave him a problem to test his knowledge. He asked Sherlock to generate a string S of L + R characters, in which there are a total of L left parentheses (
and a total ofR right parentheses )
. Moreover, the string must have as many different balanced non-empty substrings as possible. (Two substrings are considered different as long as they start or end at different indexes of the string, even if their content happens to be the same). Note that S itself does not have to be balanced.Sherlock is sure that once he knows the maximum possible number of balanced non-empty substrings, he will be able to solve the problem. Can you help him find that maximum number?
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 two integers: L and R.
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 answer, as described above.
其实就是前n项和的求解,不用多说。
B. Sherlock and Watson Gym Secrets
Problem
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.分析:这题若是暴力求解,一是复杂度为O(N^2),二是当大数据输入的时候会溢出。这肯定是不行的。
当我们考虑 iA % K 时,我们只需考虑(i%K)A % K。i 的范围是[1, N], i%K 的范围是 [0, K-1],(i%K)A % K 的范围是 [0, K-1]。我们的做法是,分别算出(i%K)A % K 值为0~K-1的个数存入数组sumA[K], (i%K)B % K 值为[0, k-1]的个数存入数组sumB[K]。那么res = sumA[0] * sumB[0] + sumA[1] * sumB[k-1] + sumA[2]*sumB[K-2]+.......就是总的满足条件的(i,j)对数。另外,题目中要求(i, j)不等,所以res还要剔除当((i%K)A % K + (i%K)B % K)% k == 0的情况。
1 #include2 using namespace std; 3 4 typedef long long LL; 5 const double EPS = 1e-6; 6 const double PI = acos(-1.0); 7 const int INF = 0x3f3f3f3f; 8 const LL MOD = 1e9+7; 9 10 template inline T bigMod(T p, T e, T M){11 long long ret = 1;12 for(; e > 0; e >>= 1){13 if(e & 1) ret = (ret * p) % M;14 p = (p * p) % M;15 } return (T)ret % M; // Attention: bigMod(p, 0, 1), so ret has to module M.16 }17 template inline T modInverse(T a, T M){ return bigMod(a,M-2,M);}18 template inline T gcd(T a, T b){ return b ? gcd(b,a%b) : a;}19 20 21 LL ar[100001], Av[100001], Bv[100001], sumA[100001], sumB[100001];22 int main() {23 ios_base::sync_with_stdio(0); cin.tie(0);24 freopen("C:\\Users\\Administrator\\Desktop\\ClionTest\\B-large-practice.in", "r", stdin);25 freopen("C:\\Users\\Administrator\\Desktop\\ClionTest\\B-large-practice.out", "w", stdout);26 int T; cin >> T;27 for (int ts = 1; ts < T + 1; ++ts) {28 memset(ar, 0, sizeof(ar)); memset(sumA, 0, sizeof(sumA)); memset(sumB, 0, sizeof(sumB));29 LL A, B, N, K; cin >> A >> B >> N >> K;30 ar[0]--;31 for (int i = 0; i < K ; ++i) {32 ar[i] += N/K;33 if (i <= (N%K)) ar[i]++; // ar[i]: 1~N之间,余数为i的个数34 Av[i] = bigMod((LL)i, A, K); // i^A%K35 Bv[i] = bigMod((LL)i, B, K); // i^B%K36 sumA[Av[i]] = (sumA[Av[i]] + ar[i]) % MOD; //余数为Av[i]的个数37 sumB[Bv[i]] = (sumB[Bv[i]] + ar[i]) % MOD; //余数为Bv[i]的个数38 }39 40 LL res = 0;41 for (int i = 1; i < K; ++i) {42 res = (res + sumA[i] * sumB[K-i]) % MOD; // (i + K - i) % K == 0, 计算个数43 }44 res = (res + sumA[0] * sumB[0]) % MOD;45 for (int i = 0; i < K; ++i) { // 剔除(first, second)中first == second的情况46 if ((Av[i] + Bv[i]) % K == 0) res = (res + MOD - (ar[i]%MOD)) % MOD;47 }48 printf("Case #%d: %lld\n", ts, res);49 }50 return 0;51 }
先写这么多,好困。。。