Codeforces Round #443 (Div. 2)

                              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
3
2 2
1 2
2 2
Output
4
Input
2
10 1
6 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 n;
 5 
 6 int main()
 7 {   cin>>n;
 8     int a,b;
 9     cin>>a>>b;
10     int temp=a;
11     for(int i=2;i<=n;i++){
12         cin>>a>>b;
13         if(a<=temp) temp=((temp-a)/b+1)*b+a;
14         else temp=a;         //竟然忘了写else,wa了一发!!!
15     }
16     cout<<temp<<endl;
17     return 0;
18 }

                        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 after which a player leaves, respectively.

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 2
1 2
Output
2 
Input
4 2
3 1 2 4
Output
3 
Input
6 2
6 5 3 1 2 4
Output
6 
Input
2 10000000000
2 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次就行了(orzzzzz)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 ll n,k;
 6 int a[505];
 7 
 8 int main()
 9 {   cin>>n>>k;
10     for(int i=1;i<=n;i++) cin>>a[i];
11     int temp=a[1],cnt=0,ans;
12     for(int i=2;i<=n;i++){
13         if(temp>a[i]) cnt++;
14         else{ temp=a[i]; cnt=1; }      //已经比较了一次,不能写成cnt=0;被别人huck了!!!
15         if(temp==n){ ans=n; break; }
16         if(cnt==k){ ans=temp; break; } 
17     }
18     cout<<ans<<endl;
19 }

                                    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.

题解:绝对好题,对于任意的一个数,换成二进制,每一位都只有0和1两种情况,那么令x=0,y=1023。通过x的n次变化和y的n次变化后,可以推断出这n次变化的等价形式。详解:http://blog.csdn.net/mengxiang000000/article/details/78363759?locationNum=10&fps=1

 1 #include<cstdio>
 2 #include<string>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int x,n;
 9 
10 int main()
11 {   cin>>n;
12     char a;
13     int Fill_0=0,Fill_1=1023;
14     for(int i=1;i<=n;i++){
15         cin>>a>>x;
16         if(a=='&'){ Fill_0&=x; Fill_1&=x; }
17         if(a=='|'){ Fill_0|=x; Fill_1|=x; }
18         if(a=='^'){ Fill_0^=x; Fill_1^=x; } 
19     }
20     int AND,XOR,OR,base=1;
21     AND=XOR=OR=0;
22     for(int i=1;i<=10;i++){
23         int num0=Fill_0&1;
24         int num1=Fill_1&1;
25         if(num0==0&&num1==1) AND+=base;
26         else if(num0==1&&num1==1){ AND+=base; OR+=base; } 
27         else if(num0==1&&num1==0){ AND+=base; XOR+=base; } 
28         Fill_0>>=1;
29         Fill_1>>=1;
30         base<<=1;
31     }
32     printf("3
^ %d
& %d
| %d
",XOR,AND,OR);
33 }

                             D. Teams Formation

This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai.

Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times).

After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city.

Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.

Input

The first line contains three integers n, k and m (1 ≤ n ≤ 105, 2 ≤ k ≤ 109, 1 ≤ m ≤ 109).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105), where ai is the number of city, person from which must take seat i in the bus.

Output

Output the number of remaining participants in the line.

Examples
Input
4 2 5
1 2 3 1
Output
12
Input
1 9 10
1
Output
1
Input
3 2 10
1 2 1
Output
0
Note

In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.

 题解:https://www.cnblogs.com/AOQNRMGYXLMV/p/7747876.html

不是很明白:中间循环的部分,为啥有两种颜色时,在m次循环的情况下肯定不存在连续的k个相同的颜色???

学习,继续学习,orz(好题)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 
 5 const int maxn=1e5+5;
 6 
 7 int n,k,m;
 8 int a[maxn],S[maxn],cnt[maxn],top; 
 9 
10 int main()
11 {   cin>>n>>k>>m;
12     bool one_color=true;
13     for(int i=1;i<=n;i++) cin>>a[i];
14     for(int i=2;i<=n;i++) if(a[i]!=a[i-1]){ one_color=false; break; } 
15     
16     if(one_color){
17         cout<<(ll)n*m%k<<endl;
18         return 0;
19     }
20     //消除内部的块 
21     for(int i=1;i<=n;i++){
22         S[++top]=a[i];
23         if(top>1&&S[top]==S[top-1]) cnt[top]=cnt[top-1]+1;
24         else cnt[top]=1;
25         if(cnt[top]>=k) top=top-k;
26     }
27 
28     int L=1,R=top;
29     //得到中间循环出现的部分 
30     ll t=0;
31     while(S[L]==S[R]&&L<R){
32         int l=L,r=R,cnt=0;
33         while(S[l]==S[L]&&l<r&&cnt<k){ cnt++; l++; } 
34         while(S[r]==S[L]&&l<r&&cnt<k){ cnt++; r--; }
35         if(cnt==k){ L=l; R=r; t+=k; }
36         else break; 
37     } 
38     
39     one_color=true;
40     for(int i=L;i<R;i++) if(S[i]!=S[i+1]){ one_color=false; break; }
41     if(one_color){                            //中间只有一种颜色 
42         ll mid=(ll)(R-L+1)*m%k;
43         if(mid) cout<<mid+t<<endl;
44         else cout<<0<<endl; 
45     }
46     else cout<<(ll)(R-L+1)*m+t<<endl;        
47 }
原文地址:https://www.cnblogs.com/zgglj-com/p/7741072.html