国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

Round E APAC Test 2017 Problem A. Diwali lightings (C++)

2019-11-08 02:15:22
字體:
來源:轉載
供稿:網友

PRoblem

Diwali is the festival of lights. To celebrate it, people decorate their houses with multi-color lights and burst crackers. Everyone loves Diwali, and so does Pari. Pari is very fond of lights, and has transfinite powers, so she buys an infinite number of red and blue light bulbs. As a programmer, she also loves patterns, so she arranges her lights by infinitely repeating a given finite pattern S.

For example, if S is BBRB, the infinite sequence Pari builds would be BBRBBBRBBBRB…

Blue is Pari’s favorite color, so she wants to know the number of blue bulbs between the Ith bulb and Jth bulb, inclusive, in the infinite sequence she built (lights are numbered with consecutive integers starting from 1). In the sequence above, the indices would be numbered as follows:

B B R B B B R B B B R B… 1 2 3 4 5 6 7 8 9 10 11 12 So, for example, there are 4 blue lights between the 4th and 8th positions, but only 2 between the 10th and 12th.

Since the sequence can be very long, she wrote a program to do the count for her. Can you do the same?

Input

The first line of the input gives the number of test cases, T. T test cases follow. First line of each test case consists of a string S, denoting the initial finite pattern. Second line of each test case consists of two space separated integers I and J, defined 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 number of blue bulbs between the Ith bulb and Jth bulb of Pari’s infinite sequence, inclusive.

Limits

1 ≤ T ≤ 100. 1 ≤ length of S ≤ 100. Each character of S is either uppercase B or uppercase R.

Small dataset

1 ≤ I ≤ J ≤ 10^6. Large dataset

1 ≤ I ≤ J ≤ 10^18.

分析:

先統計一個pattern中出現‘B’的次數。(J-I+1)/pattern.size()=pattern在[I,J]之間出現次數(J-I+1)%pattern.size()=還有多少個字母沒有考慮進來找到pattern中要考慮的起始位置cur,向后考慮(J-I+1)%pattern.size()個字母即可。(注意越界)

代碼:Github

#include <iostream>#include <math.h>#include <vector>#include <algorithm>#include <deque>#include <string>#include <stdlib.h>#include <fstream> using namespace std;typedef long long ll;int T;int main() { ifstream fin("A-large-practice.in"); ofstream fout("output"); fin >> T; for (int k = 1; k <= T; k++) { string pat = ""; fin >> pat; ll a, b; fin >> a >> b; int size = pat.size(); int num = 0; for (int i = 0; i < pat.size(); i++) { if (pat[i] == 'B') num++; } ll yu = (b - a + 1) % size; ll count = (b - a + 1) / size; ll result = num*count; ll cur = (a - 1) % size; for (int i = 1; i <= yu; i++) { if (pat[cur] == 'B') result++; cur = (cur + 1) % size; } fout << "Case #" << k << ": " << result << endl; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 积石山| 平潭县| 平果县| 岗巴县| 闸北区| 东港市| 碌曲县| 盈江县| 西藏| 昭平县| 镇赉县| 祁阳县| 武鸣县| 浪卡子县| 枝江市| 醴陵市| 广宗县| 思南县| 泸溪县| 怀集县| 买车| 安塞县| 南阳市| 贺兰县| 太白县| 苍南县| 团风县| 东乡族自治县| 正镶白旗| 南投县| 大宁县| 福泉市| 松阳县| 汉沽区| 高唐县| 香格里拉县| 楚雄市| 文登市| 托克托县| 山阴县| 城口县|