博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Round B APAC Test 2017
阅读量:5247 次
发布时间:2019-06-14

本文共 5310 字,大约阅读时间需要 17 分钟。

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, TT 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, AB, 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, TT test cases follow. Each test case consists of one line with 4 integers ABN 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)% K。i 的范围是[1, N], i%K 的范围是 [0, K-1],(i%K)% K 的范围是 [0, K-1]。我们的做法是,分别算出(i%K)% K 值为0~K-1的个数存入数组sumA[K], (i%K)% 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)% K + (i%K)% K)% k == 0的情况。

1 #include 
2 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 }

先写这么多,好困。。。

转载于:https://www.cnblogs.com/letgo/p/5860603.html

你可能感兴趣的文章
用 NodeJS 实现 BigPipe
查看>>
Orange's笔记(1)
查看>>
python集合,深浅copy
查看>>
java 图片处理工具类
查看>>
gearman简介及安装使用
查看>>
login.php织梦进不去,dedecms后台登录成功后进入不了后台的最终解决方法
查看>>
php二维数据签名验证,sing 签名验证
查看>>
matlab实验四循环结构程序设计,MATLAB实验四_循环结构程序设计.doc
查看>>
oracle dataguard详细,Oracle 11G DataGuard重启详细过程
查看>>
墨刀linux桌面版,深度商店应用Kedis、微信开发者工具、UltraEdit、墨刀
查看>>
xgad加密linux,一种机载XGA视频信息采集的设计与实现.pdf
查看>>
c语言 扑克牌大小,C语言实现简易扑克牌游戏
查看>>
android sqlite3 加密,Android SQLite文件加密
查看>>
android textview settypeface,【Android初级】使用TypeFace设置TextView的文字字体(附源码)...
查看>>
html页面引入ts文件,如何将ts文件中收到的参数显示到html页面
查看>>
html伪类元素居中,总结css伪类的几种常见操作
查看>>
html5 xdwlnjs cn,最近需要调用一个网站的js,但是发现是加密的,有大佬来解密下吗?...
查看>>
html字体样式源码,css文字样式中属性的介绍(附代码)
查看>>
html伪元素before占用高度,使用before伪元素出现的问题?
查看>>
mysql 编码php,php-MySQL的编码问题(和基础知识)
查看>>