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数组任意两个数的差 ...
随机推荐
- 切点算法模板(Cut-vertex)
下面是一个模板被切割点,也cut_vertex_num[]排列(array)什么是切 - 点记录 Int cut_vertex_num[]; void dfs(int cur,int pa) { in ...
- SQL数据库插入文本信息
文本内容
- Java程序在向mysql中插入数据的时候出现乱码
今天在往数据库中插入数据的时候中文字符在数据库中就出现了乱码?网上有各种说法,但是适合我的,最终解决我的问题的只有下面一种! 在创建数据库的时候,注意设置编码方式. CREATE DATABASE ` ...
- 怎样在UICollectionView中添加Header和footer
---恢复内容开始--- 怎样在UICollectionView中添加Header和footer 转载于http://my.oschina.net/zboy/blog/221525 摘要 来自-htt ...
- BZOJ 1407: [Noi2002]Savage( 数论 )
枚举答案, 然后O(N^2)枚举野人去判他们是否会在有生之年存在同山洞. 具体做法就是: 设第x年相遇, 则 Ci+x*Pi=Cj+x*Pj (mod M), 然后解同余方程. 复杂度应该是O(ans ...
- linux杂记(四)热键[Tab],[ctrl]-c,[ctrl]-d,在线求助man page/info page
[Tab]按键 他具有[命令补全](接在一串指令的第一个字后面)与[档案补齐](接在第一串指令的第二字以后时)的功能.如 [KANO@kelvin ~]$ ca[tab][tab] cabextrac ...
- 利用python进行数据分析之数据加载存储与文件格式
在开始学习之前,我们需要安装pandas模块.由于我安装的python的版本是2.7,故我们在https://pypi.python.org/pypi/pandas/0.16.2/#downloads ...
- halcon与C#混合编程进阶版
这篇主要是C#和Halcon的混合编程,在此基础上对按键不同功能的划分,以及图片适应窗口和从本地打开图片. 新手来这里:http://www.cnblogs.com/badguy518/p/55150 ...
- android studio2.0 搭建Robotium环境--有被测源代码的情况下
1.导入robotium-solo-5.2.1.jar 包 导入junit:4.12.jar2.app- -src- -main- -libs 或者app-libs下 复制进去后,右键add a ...
- 数学之路(3)-机器学习(3)-机器学习算法-SVM[7]
SVM是新近出现的强大的数据挖掘工具,它在文本分类.手写文字识别.图像分类.生物序列分析等实际应用中表现出非常好的性能.SVM属于监督学习算法,样本以属性向量的形式提供,所以输入空间是Rn的子集. 图 ...