Round B APAC Test 2017
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的情况。
#include <bits/stdc++.h>
using namespace std; typedef long long LL;
const double EPS = 1e-;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const LL MOD = 1e9+; template <class T> inline T bigMod(T p, T e, T M){
long long ret = ;
for(; e > ; e >>= ){
if(e & ) ret = (ret * p) % M;
p = (p * p) % M;
} return (T)ret % M; // Attention: bigMod(p, 0, 1), so ret has to module M.
}
template <class T> inline T modInverse(T a, T M){return bigMod(a,M-,M);}
template <class T> inline T gcd(T a, T b){return b ? gcd(b,a%b) : a;} LL ar[], Av[], Bv[], sumA[], sumB[];
int main() {
ios_base::sync_with_stdio(); cin.tie();
freopen("C:\\Users\\Administrator\\Desktop\\ClionTest\\B-large-practice.in", "r", stdin);
freopen("C:\\Users\\Administrator\\Desktop\\ClionTest\\B-large-practice.out", "w", stdout);
int T; cin >> T;
for (int ts = ; ts < T + ; ++ts) {
memset(ar, , sizeof(ar)); memset(sumA, , sizeof(sumA)); memset(sumB, , sizeof(sumB));
LL A, B, N, K; cin >> A >> B >> N >> K;
ar[]--;
for (int i = ; i < K ; ++i) {
ar[i] += N/K;
if (i <= (N%K)) ar[i]++; // ar[i]: 1~N之间,余数为i的个数
Av[i] = bigMod((LL)i, A, K); // i^A%K
Bv[i] = bigMod((LL)i, B, K); // i^B%K
sumA[Av[i]] = (sumA[Av[i]] + ar[i]) % MOD; //余数为Av[i]的个数
sumB[Bv[i]] = (sumB[Bv[i]] + ar[i]) % MOD; //余数为Bv[i]的个数
} LL res = ;
for (int i = ; i < K; ++i) {
res = (res + sumA[i] * sumB[K-i]) % MOD; // (i + K - i) % K == 0, 计算个数
}
res = (res + sumA[] * sumB[]) % MOD;
for (int i = ; i < K; ++i) { // 剔除(first, second)中first == second的情况
if ((Av[i] + Bv[i]) % K == ) res = (res + MOD - (ar[i]%MOD)) % MOD;
}
printf("Case #%d: %lld\n", ts, res);
}
return ;
}
先写这么多,好困。。。
Round B APAC Test 2017的更多相关文章
- 2017 google Round D APAC Test 题解
首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...
- 2017 google Round C APAC Test 题解
题解参考网上的答案,以及我自己的想法. 主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/4 ...
- <Google><APAC><kickstart><2017.05.07><2017RoundB>
Google APAC kickstart 网址链接 我的所有solution代码和文件请点击 前言 这个比赛的题怎一个变态了得,虽然是第一次参赛,抱着熟悉流程的心态去的,但仍然被虐得一颤一颤的╮(╯ ...
- 2017 Bangladesh National High School Programming Contest ( National Round, Senior Group ), NHSPC 2017 题解
[题目链接] A. Charm Is Not Always Enough 模拟一下就可以了. #include <bits/stdc++.h> using namespace std; i ...
- [ Google APAC 2015 University Graduates Test ] Round C APAC Test
题目链接: http://code.google.com/codejam/contest/5214486/dashboard Problem A. Minesweeper 题目意思: 扫雷.告诉地雷所 ...
- PYTHON3 中的虚假四舍五入:round()
PYTHON3 中的虚假四舍五入:round() 创建时间: 2017/12/5 17:08 作者: CN_Simo 标签: python基础, round, 四舍五入 一.这不是一个BUG! 在使用 ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)
A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...
- 2017 Russian Code Cup (RCC 17), Final Round
2017 Russian Code Cup (RCC 17), Final Round A Set Theory 思路:原题转换一下就是找一个b数组,使得b数组任意两个数的差值都和a数组任意两个数的差 ...
随机推荐
- css中的media
说起CSS3的新特性,就不得不提到 Media Queries .最近 Max Design 更新的一个泛读列表里,赫然就有关于 Media Queries 的文章.同时位列其中的也有前天我刚刚翻译的 ...
- Excel几个常用操作
1.冻结第一行:视图->冻结窗格->冻结首行 2.第一行自动筛选:选择要设置的行,数据->筛选->自动筛选 3.在单元格中添加下拉菜单:(1)选中一列,数据->数据验证. ...
- Python 读写文件操作
python进行文件读写的函数是open或file file_handler = open(filename,,mode) Table mode 模式 描述 r 以读方式打开文件,可读取文件信息. w ...
- C# Attribute(特性)之---数据契约 [DataContract]
服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...
- unexpected token: null near line 1, column 290
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: null near line 1, column 290 ...
- Spring总结——控制反转,注入(配置和注解两种方式)
一.Spring的容器: 1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的(也可以说类A依赖类B),而在Spring中,则是由s ...
- Mongodb安装和基本命令
本人是在Centos中安装的mongodb 1.下载mongodb curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2. ...
- java实现cmd的copy功能
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- nyist 500 一字棋
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=500 这太并不难,只要把情况分清楚就可以了,本人由于考虑不是很周全,WA了n次....悲 ...
- SQL Server 内存不足引起的并发症
第一:cpu 1.内存不足就会有频繁的页面调入调出.这个过程是要有cpu的参与的.所以这个要影响cpu! 2.内存不足可能会引有用起执行计划被清除.当起次要执行时.这个就要重编译一次!