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数组任意两个数的差 ...
随机推荐
- The Swift Programming Language--语言指南--协议
Protocol(协议)用于统一方法和属性的名称,而不实现任何功能.协议能够被类,枚举,结构体实现,满足协议要求的类,枚举,结构体被称为协议的遵循者. 遵循者需要提供协议指定的成员,如属性,方法, ...
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- spring拦截器的定义
(一).拦截器的定义 1.为什么需要拦截器:在做身份认证或者是进行日志的记录时,我们需要通过拦截器达到我们的目的 2.什么事拦截器:在AOP(Aspect-Oriented Programming)中 ...
- shell检测interface是否已分配ip,qt调用shell脚本
#include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...
- C++_基础_类和对象2
内容: (1)构造函数 (2)初始化列表及其必要性 (3)支持自定义类型转换的构造函数 (4)this指针 (5)const对象和成员函数 (6)析构函数 1.构造函数1.1 格式: class 类名 ...
- mysql的触发器
删除触发器 drop TRIGGER 触发器名字; 查找库里面的所有触发器 SELECT * FROM information_schema.`TRIGGERS`;show triggers 触发器语 ...
- (转) ios学习之 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
刚接触iOS开发的人难免会对苹果的各种证书.配置文件等不甚了解,可能你按照网上的教程一步一步的成功申请了真机调试,但是还是对其中的缘由一知半解.这篇文章就对Certificate.Provisioni ...
- Fiddler [Fiddler] Connection to localhost. failed.
原文地址:http://blog.chinaunix.net/uid-20675015-id-1899931.html 在用Fiddler调试本机的网站时,即访问http://localhost,返回 ...
- [LeetCode]题解(python):143-Reorder List
题目来源: https://leetcode.com/problems/reorder-list/ 题意分析: 给定一个链表L:L0→L1→…→Ln-1→Ln,改变链表的排序为: L0→Ln→L1→L ...
- SSD的基本架构
在SSD的优势一章中,我们对比过HDD和SSD的内部区别.现在,我们再谈一下SSD的基本架构. 上图为一款典型的SSD架构图解,各部分的解释如下: 操作 ...