编程介的小学生 2017-03-23 12:13 采纳率: 20.5%
浏览 772
已采纳

Edison

It is said that Columbus once invented card games. After thousands of years, all kinds of card games have spread all over the world. Shuffling the deck is an inevitable task, because no one wants to play the same game twice. There are usually 54 cards in a deck, and we usually shuffle the deck by repeatedly taking out a sequence of cards and put them onto the top of the deck. The following figure illustrates the effect of a single shuffle.

A single shuffle with P = 2 and L = 3 will move the white cards onto the top

To help people shuffle, Edison invented a machine, namely Automated Card-shuffling Machine. The machine is controlled by instructions, which give the starting position P and the length L of the sequence. Of course the instructions are generated by a randomizer. Unfortunately it is discovered that the randomizer has a bug that leads to repetitive instructions. That is, several consecutive instructions may be exactly the same. Though despaired, Edison decides to pack the repetitive instructions into a single "extended instruction" and ship this machine off to the market. An extended instruction consists of not only P and L for a sequence, but also the number of times R it repeats.

Input Description

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

The first line of each test case contains two integers C (0 < C <= 1,000,000) and S (0 <= S <= 5,000). The deck holds C cards, numbered from 0 to C - 1, and we will do S shuffles.

The following S lines contain three integers each, Pi (0 <= Pi < C), Li (1 <= Li <= C) and Ri (1 <= Ri <= 100), 1 <= i <= S. Each line represents an extended instruction. Note that the card on the top has a position of 0. It is guaranteed that Pi + Li <= C.

Output Description

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

For each test case, print the sum of the card numbers at odd positions of deck after shuffling.

Sample Input
2

10 1
5 3 1

10 3
5 3 1
2 4 1
7 2 2

Sample Output
Case 1:
24

Case 2:
23

HINT: The cards on the decks after shuffling would be:
Case 1 �C 5 6 7 0 1 2 3 4 8 9 (5 + 7 + 1 + 3 + 8 = 24)
Case 2 �C 6 3 4 8 7 0 1 2 5 9 (6 + 4 + 7 + 1 + 5 = 23)

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?