CF207A3.Beaver's Calculator 1.0

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Smart Beaver from ABBYY has once again surprised us! He has developed a new calculating device, which he called the "Beaver's Calculator 1.01.0 ". It is very peculiar and it is planned to be used in a variety of scientific problems.

To test it, the Smart Beaver invited nn scientists, numbered from 11 to nn . The ii -th scientist brought kik_{i} calculating problems for the device developed by the Smart Beaver from ABBYY. The problems of the ii -th scientist are numbered from 11 to kik_{i} , and they must be calculated sequentially in the described order, since calculating each problem heavily depends on the results of calculating of the previous ones.

Each problem of each of the nn scientists is described by one integer ai,ja_{i,j} , where ii ( 1<=i<=n1<=i<=n ) is the number of the scientist, jj ( 1<=j<=ki1<=j<=k_{i} ) is the number of the problem, and ai,ja_{i,j} is the number of resource units the calculating device needs to solve this problem.

The calculating device that is developed by the Smart Beaver is pretty unusual. It solves problems sequentially, one after another. After some problem is solved and before the next one is considered, the calculating device allocates or frees resources.

The most expensive operation for the calculating device is freeing resources, which works much slower than allocating them. It is therefore desirable that each next problem for the calculating device requires no less resources than the previous one.

You are given the information about the problems the scientists offered for the testing. You need to arrange these problems in such an order that the number of adjacent "bad" pairs of problems in this list is minimum possible. We will call two consecutive problems in this list a "bad pair" if the problem that is performed first requires more resources than the one that goes after it. Do not forget that the problems of the same scientist must be solved in a fixed order.

输入格式

The first line contains integer nn — the number of scientists. To lessen the size of the input, each of the next nn lines contains five integers kik_{i} , ai,1a_{i,1} , xix_{i} , yiy_{i} , mim_{i} ( 0<=ai,1<mi<=1090<=a_{i,1}<m_{i}<=10^{9} , 1<=xi,yi<=1091<=x_{i},y_{i}<=10^{9} ) — the number of problems of the ii -th scientist, the resources the first problem requires and three parameters that generate the subsequent values of ai,ja_{i,j} . For all jj from 22 to kik_{i} , inclusive, you should calculate value ai,ja_{i,j} by formula ai,j=(ai,j1xi+yi)a_{i,j}=(a_{i,j-1}*x_{i}+y_{i}) modmod mim_{i} , where aa modmod bb is the operation of taking the remainder of division of number aa by number bb .

To get the full points for the first group of tests it is sufficient to solve the problem with n=2n=2 , 1<=ki<=20001<=k_{i}<=2000 .

To get the full points for the second group of tests it is sufficient to solve the problem with n=2n=2 , 1<=ki<=2000001<=k_{i}<=200000 .

To get the full points for the third group of tests it is sufficient to solve the problem with 1<=n<=50001<=n<=5000 , 1<=ki<=50001<=k_{i}<=5000 .

输出格式

On the first line print a single number — the number of "bad" pairs in the optimal order.

If the total number of problems does not exceed 200000200000 , also print lines — the optimal order of the problems. On each of these lines print two integers separated by a single space — the required number of resources for the problem and the number of the scientist who offered this problem, respectively. The scientists are numbered from 11 to nn in the order of input.

输入输出样例

  • 输入#1

    2
    2 1 1 1 10
    2 3 1 1 10
    

    输出#1

    0
    1 1
    2 1
    3 2
    4 2
    
  • 输入#2

    2
    3 10 2 3 1000
    3 100 1 999 1000
    

    输出#2

    2
    10 1
    23 1
    49 1
    100 2
    99 2
    98 2
    

说明/提示

In the first sample n=2n=2 , k1=2k_{1}=2 , a1,1=1a_{1,1}=1 , a1,2=2a_{1,2}=2 , k2=2k_{2}=2 , a2,1=3a_{2,1}=3 , a2,2=4a_{2,2}=4 . We've got two scientists, each of them has two calculating problems. The problems of the first scientist require 11 and 22 resource units, the problems of the second one require 33 and 44 resource units. Let's list all possible variants of the calculating order (each problem is characterized only by the number of resource units it requires): (1,2,3,4)(1,2,3,4) , (1,3,2,4)(1,3,2,4) , (3,1,2,4)(3,1,2,4) , (1,3,4,2)(1,3,4,2) , (3,4,1,2)(3,4,1,2) , (3,1,4,2)(3,1,4,2) .

Sequence of problems (1,3,2,4)(1,3,2,4) has one "bad" pair ( 33 and 22 ), (3,1,4,2)(3,1,4,2) has two "bad" pairs ( 33 and 11 , 44 and 22 ), and (1,2,3,4)(1,2,3,4) has no "bad" pairs.

首页