A. Borya‘s Diagnosis
It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.
Doctors have a strange working schedule. The doctor i goes to work on the si-th day and works every di day. So, he works on days si, si + di, si + 2di, ....
The doctor‘s appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors?
Input
First line contains an integer n — number of doctors (1 ≤ n ≤ 1000).
Next n lines contain two numbers si and di (1 ≤ si, di ≤ 1000).
Output
Output a single integer — the minimum day at which Borya can visit the last doctor.
Examples
Input
32 21 22 2
Output
4
Input
210 16 5
Output
11
Note
In the first sample case, Borya can visit all doctors on days 2, 3 and 4.
In the second sample case, Borya can visit all doctors on days 10 and 11.
模拟一下。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() { 5 int n, ans = 0; 6 cin >> n; 7 while(n--) { 8 int s, d; 9 cin >> s >>d; 10 while(ans >= s) s += d; 11 ans = s; 12 } 13 printf("%d\n",ans); 14 return 0; 15 }
B. Table Tennis
n
people are standing in a line to play table tennis. At first, the first
two players in the line play a game. Then the loser goes to the end of
the line, and the winner plays with the next person from the line, and
so on. They play until someone wins k games in a row. This player becomes the winner.
For
each of the participants, you know the power to play table tennis, and
for all players these values are different. In a game the player with
greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — powers of the player. It‘s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer — power of the winner.
Examples
Input
2 21 2
Output
2
Input
4 23 1 2 4
Output
3
Input
6 26 5 3 1 2 4
Output
6
Input
2 100000000002 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
当k>= n-1时,答案一定是n,剩下的就去模拟一下,不过要注意的是i != 0时,要判断下是否大于前面的。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const int N = 550; 5 int a[N]; 6 int main() { 7 ll n, k; 8 cin >> n >> k; 9 for(int i = 0; i < n; i ++) cin >> a[i]; 10 if(k >= n) printf("%d\n",n); 11 else { 12 for(int i = 0; i < n; i ++) { 13 int ans = 0; 14 if(i != 0 && a[i] > a[i-1]) ans++; 15 for(int j = 1; j <= n; j ++) { 16 if(a[i] > a[(i+j)%n]) ans ++; 17 if(ans >= k) return 0*printf("%d\n",a[i]); 18 if(a[i] <= a[(i+j)%n]) break; 19 } 20 } 21 } 22 return 0; 23 }
C. Short Program
Petya
learned a new programming language CALPAS. A program in this language
always takes one non-negative integer and returns one non-negative
integer as well.
In the language, there are only three commands:
apply a bitwise operation AND, OR or XOR with a given constant to the
current integer. A program can contain an arbitrary sequence of these
operations with arbitrary constants from 0 to 1023.
When the program is run, all operations are applied (in the given
order) to the argument and in the end the result integer is returned.
Petya
wrote a program in this language, but it turned out to be too long.
Write a program in CALPAS that does the same thing as the Petya‘s
program, and consists of no more than 5 lines. Your program should return the same integer as Petya‘s program for all arguments from 0 to 1023.
Input
The first line contains an integer n (1 ≤ n ≤ 5·105) — the number of lines.
Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 ≤ xi ≤ 1023.
Output
Output an integer k (0 ≤ k ≤ 5) — the length of your program.
Next k lines must contain commands in the same format as in the input.
Examples
Input
3| 3^ 2| 1
Output
2| 3^ 2
Input
3& 1& 3& 5
Output
1& 1
Input
3^ 1^ 2^ 3
Output
0
Note
You can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.
Second sample:
Let x be an input of the Petya‘s program. It‘s output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.
所有的操作都可以转换为(((x^a)|b)&c)。 由于范围在0-1023 所以遍历第1位到第9位上的值就行。
x=0表示 每一位上的都是0,y=1023表示每一位上的都是1。
(x&(1<<i)) > 0 表示 x变成了1 (x&(1<<i)) == 0 表示x变成了0。当是y时同理。
所以有四种情况, 每一位都可以表示 (((z^a)|b)&c)
x y a b c
0->0 1->0 0 0 0
0->0 1->1 0 0 1
0->1 1->0 1 0 1
0->1 1->1 0 1 1
所以答案就出来了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 char str[10]; 4 int main() { 5 int n, tmp, x = 0, y = 1023; 6 cin >> n; 7 for(int i = 1; i <= n; i ++) { 8 cin >> str >> tmp; 9 if(str[0] == ‘|‘) { 10 x |= tmp; 11 y |= tmp; 12 } else if(str[0] == ‘^‘) { 13 x ^= tmp; 14 y ^= tmp; 15 } else { 16 x &= tmp; 17 y &= tmp; 18 } 19 } 20 int a = 0, b = 0, c = 0; 21 for(int i = 0; i < 10; i ++) { 22 if((x&(1<<i)) > 0 && (y&(1<<i)) > 0) { 23 b += (1<<i); 24 c += (1<<i); 25 } else if((x&(1<<i)) > 0 && (y&(1<<i)) == 0) { 26 a += (1<<i); 27 c += (1<<i); 28 } else if((x&(1<<i)) == 0 && (y&(1<<i)) > 0) { 29 c += (1<<i); 30 } else { 31 32 } 33 } 34 printf("3\n"); 35 printf("^ %d\n", a); 36 printf("| %d\n", b); 37 printf("& %d\n", c); 38 return 0; 39 }
B. Table Tennis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
n
people are standing in a line to play table tennis. At first, the first
two players in the line play a game. Then the loser goes to the end of
the line, and the winner plays with the next person from the line, and
so on. They play until someone wins k games in a row. This player becomes the winner.
For
each of the participants, you know the power to play table tennis, and
for all players these values are different. In a game the player with
greater power always wins. Determine who will be the winner.
Input
The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — powers of the player. It‘s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.
Output
Output a single integer — power of the winner.
Examples
Input
2 21 2
Output
2
Input
4 23 1 2 4
Output
3
Input
6 26 5 3 1 2 4
Output
6
Input
2 100000000002 1
Output
2
Note
Games in the second sample:
3 plays with 1. 3 wins. 1 goes to the end of the line.
3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.