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的情况。

 #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的更多相关文章

  1. 2017 google Round D APAC Test 题解

    首先说明一下:我只是用暴力过了4道题的小数据,就是简单的枚举,大数据都不会做!下面的题解,是我从网上搜到的解答以及查看排行榜上大神的答案得出来的. 首先贴一下主要的题解来源:http://codefo ...

  2. 2017 google Round C APAC Test 题解

    题解参考网上的答案,以及我自己的想法. 主要参考网站:http://codeforces.com/blog/entry/47181,http://codeforces.com/blog/entry/4 ...

  3. <Google><APAC><kickstart><2017.05.07><2017RoundB>

    Google APAC kickstart 网址链接 我的所有solution代码和文件请点击 前言 这个比赛的题怎一个变态了得,虽然是第一次参赛,抱着熟悉流程的心态去的,但仍然被虐得一颤一颤的╮(╯ ...

  4. 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 ...

  5. [ Google APAC 2015 University Graduates Test ] Round C APAC Test

    题目链接: http://code.google.com/codejam/contest/5214486/dashboard Problem A. Minesweeper 题目意思: 扫雷.告诉地雷所 ...

  6. PYTHON3 中的虚假四舍五入:round()

    PYTHON3 中的虚假四舍五入:round() 创建时间: 2017/12/5 17:08 作者: CN_Simo 标签: python基础, round, 四舍五入 一.这不是一个BUG! 在使用 ...

  7. 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 ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  9. 2017 Russian Code Cup (RCC 17), Final Round

    2017 Russian Code Cup (RCC 17), Final Round A Set Theory 思路:原题转换一下就是找一个b数组,使得b数组任意两个数的差值都和a数组任意两个数的差 ...

随机推荐

  1. md笔记——微信JS接口

    微信js接口 隐藏微信中网页右上角按钮 document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { Weix ...

  2. IIS7.5(IIS7)配置伪静态urlrewrite

    找了好久,终于找到了.已经测试通过,收藏. 转载自:http://jingyan.baidu.com/article/67508eb4ff92c69cca1ce49a.html 首先新建一个应用程序池 ...

  3. oracle rac 安装脚本

    1. 配置/etc/hosts 网络 192.168.1.111 rac1 rac1.oracle.com192.168.1.182 rac1-vip 192.168.1.222 rac2 rac2. ...

  4. MVC打开电脑对话框

    //下载文件 public ActionResult Download(int id) { //依靠模板生成文档 var path =要下载的文件的路径 var name = Path.GetFile ...

  5. 优秀的 Android Studio 插件

    转自:http://www.codeceo.com/article/8-android-studio-plugins.html Android Studio是目前Google官方设计的用于原生Andr ...

  6. tag标签添加删除并把值存入到一个input的value内

    html: <input type="text" id="tagValue" style="display: none;" /> ...

  7. 【转】jsp 表单form传值

    写的很好,看到了忍不住不转啊,希望可以分享一下~~ 转载自http://blog.csdn.net/anmei2010/article/details/4140216 页面间链接和数据传递的三种方式 ...

  8. 《Pointers On C》读书笔记(第二章 基本概念)

    1.从源代码到生成可执行程序的过程整体上可以分为两个阶段:编译和链接.其中,编译过程大致上又可分为:预处理.编译和汇编.预处理阶段主要对源代码中的预处理指令(包含宏定义指令<如 #define& ...

  9. NET CORE 应用程序启动

    原文:Application Startup作者:Steve Smith翻译:刘怡(AlexLEWIS)校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程序提供 ...

  10. SQL Server Mysql primary key可更新性分析

    SQL Server: 一般来说SQL Server 中表的主键是支持更新操作的.但是如果这个主键是由identity(1,1)这类的方式生成的话它是不可更新的. Mysql : Mysql 中表的主 ...