A. Diplomas and Certificates
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n students who have taken part in an olympiad. Now it's time to award the students.

Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly k times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of n). It's possible that there are no winners.

You have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners.

Input

The first (and the only) line of input contains two integers n and k (1 ≤ n, k ≤ 1012), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas.

Output

Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.

It's possible that there are no winners.

Examples
Input
18 2
Output
3 6 9
Input
9 10
Output
0 0 9
Input
1000000000000 5
Output
83333333333 416666666665 500000000002
Input
1000000000000 499999999999
Output
1 499999999999 500000000000题意：题解：水
 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<set>
11 #define ll __int64
12 #define mod 100000000
13 using namespace std;
14 ll n,k;
15 int main()
16 {
17     scanf("%I64d %I64d",&n,&k);
18     ll now=n/2/(1+k);
19     printf("%I64d %I64d %I64d",now,now*k,n-(1+k)*now);
20     return 0;
21 }

B. Permutation Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.

The game consists of m steps. On each step the current leader with index i counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.

You are given numbers l1, l2, ..., lm — indices of leaders in the beginning of each step. Child with number l1 is the first leader in the game.

Write a program which will restore a possible permutation a1, a2, ..., an. If there are multiple solutions then print any of them. If there is no solution then print -1.

Input

The first line contains two integer numbers n, m (1 ≤ n, m ≤ 100).

The second line contains m integer numbers l1, l2, ..., lm (1 ≤ li ≤ n) — indices of leaders in the beginning of each step.

Output

Print such permutation of n numbers a1, a2, ..., an that leaders in the game will be exactly l1, l2, ..., lm if all the rules are followed. If there are multiple solutions print any of them.

If there is no permutation which satisfies all described conditions print -1.

Examples
Input
4 52 3 1 4 4
Output
3 1 2 4
Input
3 33 1 2
Output
-1
Note

• Child 2 starts.
• Leadership goes from 2 to 2 + a2 = 3.
• Leadership goes from 3 to 3 + a3 = 5. As it's greater than 4, it's going in a circle to 1.
• Leadership goes from 1 to 1 + a1 = 4.
• Leadership goes from 4 to 4 + a4 = 8. Thus in circle it still remains at 4.

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<set>
11 #define ll __int64
12 #define mod 100000000
13 using namespace std;
14 int n,m;
15 int l;
16 map<int,set< int > >mp;
17 map<int,int>s;
18 int main(){
19
20     scanf("%d %d",&n,&m);
21     for(int i=1;i<=m;i++)
22         scanf("%d",&l[i]);
23     for(int i=1;i<m;i++)
24     {
25         if(l[i]<l[i+1])
26             mp[l[i]].insert((l[i+1]-l[i]));
27         else
28             mp[l[i]].insert((l[i+1]-l[i]+n));
29     }
30     for(int i=1;i<=n;i++)
31     {
32         if(mp[i].size()>1)
33         {
34             printf("-1\n");
35             return 0;
36         }
37     }
38      for(int i=1;i<=n;i++)
39      {
40          if(mp[i].size()==1)
41          {
42              int exm=*(mp[i].begin());
43              if(s[exm]==0)
44              {
45                  s[exm]=1;
46              }
47              else
48              {
49                  printf("-1\n");
50                  return 0;
51              }
52          }
53      }
54     for(int i=1;i<=n;i++)
55     {
56          if(mp[i].size()==0)
57          {
58              for(int j=1;j<=n;j++)
59              {
60                  if(s[j]==0)
61                  {
62                      mp[i].insert(j);
63                      s[j]=1;
64                      break;
65                  }
66              }
67          }
68     }
69     for(int i=1;i<=n;i++)
70         printf("%d ",*(mp[i].begin()));
71     return 0;
72 }

C. Sofa Thief
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss?

Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same!

The storehouse is represented as matrix n × m. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells.

Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B. Sofa A is standing to the top of sofa B if there exist two such cells a and b that ya < yb, a is covered by A and b is covered by B. Right and bottom conditions are declared the same way.

Note that in all conditions A ≠ B. Also some sofa A can be both to the top of another sofa B and to the bottom of it. The same is for left and right conditions.

The note also stated that there are cntl sofas to the left of Grandpa Maks's sofa, cntr — to the right, cntt — to the top and cntb — to the bottom.

Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions.

Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1.

Input

The first line contains one integer number d (1 ≤ d ≤ 105) — the number of sofas in the storehouse.

The second line contains two integer numbers n, m (1 ≤ n, m ≤ 105) — the size of the storehouse.

Next d lines contains four integer numbers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — coordinates of the i-th sofa. It is guaranteed that cells (x1, y1) and (x2, y2) have common side, (x1, y1)  ≠  (x2, y2) and no cell is covered by more than one sofa.

The last line contains four integer numbers cntl, cntr, cntt, cntb (0 ≤ cntl, cntr, cntt, cntb ≤ d - 1).

Output

Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through d as given in input. If there is no such sofa then print -1.

Examples
Input
23 23 1 3 21 2 2 21 0 0 1
Output
1
Input
310 101 2 1 15 5 6 56 4 5 42 1 2 0
Output
2
Input
22 22 1 1 11 2 2 21 0 0 0
Output
-1
Note

Let's consider the second example.

• The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below).
• The second sofa has cntl = 2, cntr = 1, cntt = 2 and cntb = 0.
• The third sofa has cntl = 2, cntr = 1, cntt = 1 and cntb = 1.

So the second one corresponds to the given conditions.

In the third example

• The first sofa has cntl = 1, cntr = 1, cntt = 0 and cntb = 1.
• The second sofa has cntl = 1, cntr = 1, cntt = 1 and cntb = 0.

And there is no sofa with the set (1, 0, 0, 0) so the answer is -1.

xa < xb  左

xa >xb 右

Ya < Yb 上

Ya < Yb 下

  1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<set>
11 #define ll __int64
12 #define mod 100000000
13 using namespace std;
14 int d;
15 int n,m;
16 int x1,y1;
17 int x2,y2;
18 int l,r;
19 int u,dd;
20 int suml,sumr;
21 int sumu,sumdd;
22 int b;
23 int main()
24 {
25     scanf("%d",&d);
26     scanf("%d %d",&n,&m);
27     for(int i=1;i<=d;i++){
28         scanf("%d %d %d %d",&x1[i],&y1[i],&x2[i],&y2[i]);
29         if(y1[i]==y2[i]){
30             l[y1[i]]++;
31             r[y2[i]]++;
32             if(x1[i]>x2[i]){
33                 dd[x1[i]]++;
34                 u[x2[i]]++;
35             }
36             else{
37                 dd[x2[i]]++;
38                 u[x1[i]]++;
39             }
40         }
41         if(x1[i]==x2[i]){
42          dd[x1[i]]++;
43          u[x2[i]]++;
44          if(y1[i]>y2[i]){
45              r[y1[i]]++;
46              l[y2[i]]++;
47          }
48          else{
49              r[y2[i]]++;
50              l[y1[i]]++;
51          }
52         }
53         }
54     for(int i=1;i<=4;i++)
55         scanf("%d",&b[i]);
56     suml=0;
57     for(int i=1;i<=m;i++)
58         suml[i]=suml[i-1]+l[i];
59       sumr[m+1]=0;
60     for(int i=m;i>=1;i--)
61         sumr[i]=sumr[i+1]+r[i];
62       sumu=0;
63     for(int i=1;i<=n;i++)
64         sumu[i]=sumu[i-1]+u[i];
65       sumdd[n+1]=0;
66     for(int i=n;i>=1;i--)
67         sumdd[i]=sumdd[i+1]+dd[i];
68     int num;
69     int jishu=0;
70     for(int i=1;i<=d;i++){
71         if(x1[i]==x2[i]){
72             num=sumu[x1[i]-1];
73             num=sumdd[x2[i]+1];
74             if(y1[i]<y2[i]){
75                 num=suml[y1[i]]-1;
76                 num=sumr[y2[i]]-1;
77             }
78             else{
79                  num=suml[y2[i]]-1;
80                 num=sumr[y1[i]]-1;
81             }
82             //cout<<num<<" ^^^^"<<num<<" "<<num<<" "<<num<<endl;
83         }
84         if(y1[i]==y2[i]){
85             num=suml[y1[i]-1];
86             num=sumr[y2[i]+1];
87             if(x1[i]<x2[i]){
88                 num=sumu[x1[i]]-1;
89                 num=sumdd[x2[i]]-1;
90             }
91             else{
92                 num=sumu[x2[i]]-1;
93                 num=sumdd[x1[i]]-1;
94             }
95         }
96         int flag=0;
97         for(int j=1;j<=4;j++)
98         {
99             if(num[j]==b[j])
100                 flag++;
101         }
102         if(flag==4)
103         {
104             printf("%d\n",i);
105             return 0;
106         }
107     }
108     printf("-1\n");
109     return 0;
110 }

nD. Multicolored Cars
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another.

The game rules are like this. Firstly Alice chooses some color A, then Bob chooses some color B (A ≠ B). After each car they update the number of cars of their chosen color that have run past them. Let's define this numbers after i-th car cntA(i) and cntB(i).

• If cntA(i) > cntB(i) for every i then the winner is Alice.
• If cntB(i) ≥ cntA(i) for every i then the winner is Bob.
• Otherwise it's a draw.

Bob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color A and Bob now wants to choose such color B that he will win the game (draw is not a win). Help him find this color.

If there are multiple solutions, print any of them. If there is no such color then print -1.

Input

The first line contains two integer numbers n and A (1 ≤ n ≤ 105, 1 ≤ A ≤ 106) – number of cars and the color chosen by Alice.

The second line contains n integer numbers c1, c2, ..., cn (1 ≤ ci ≤ 106) — colors of the cars that Alice and Bob will encounter in the order of their appearance.

Output

Output such color B (1 ≤ B ≤ 106) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1.

It is guaranteed that if there exists any solution then there exists solution with (1 ≤ B ≤ 106).

Examples
Input
4 12 1 4 2
Output
2
Input
5 22 2 4 5 3
Output
-1
Input
3 101 2 3
Output
4
Note

Let's consider availability of colors in the first example:

• cnt2(i) ≥ cnt1(i) for every i, and color 2 can be the answer.
• cnt4(2) < cnt1(2), so color 4 isn't the winning one for Bob.
• All the other colors also have cntj(2) < cnt1(2), thus they are not available.

In the third example every color is acceptable except for 10.

题意：给你一个长度为n的序列  要求选取一个不为A的数B  使得所有的cntB(i) ≥ cntA(i)   前i个数中B出现的次数大于A出现的次数

题解： mp[i].set()出现i次数有哪些；

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<set>
11 #define ll __int64
12 #define mod 100000000
13 using namespace std;
14 int n,A;
15 int a;
16 map<int,int>mp;
17 map<int,set< int > > se;
18 set<int>::iterator it,itt;
19 set<int > re;
20 int main()
21 {
22     scanf("%d %d",&n,&A);
23     for(int i=1;i<=n;i++)
24         scanf("%d",&a[i]);
25     int now=0;
26     int flag=0;
27     for(int i=1;i<=n;i++)
28     {
29         if(a[i]!=A){
30         it=se[mp[a[i]]].find(a[i]);
31         if(it!=se[mp[a[i]]].end())
32             se[mp[a[i]]].erase(it);
33         mp[a[i]]++;
34         se[mp[a[i]]].insert(a[i]);
35         if(flag==0)
36             re.insert(a[i]);
37         }
38         else{
39             now++;
40             if(now==1)
41                 flag=1;
42             for(it=re.begin();it!=re.end();)
43             {
44                 if(mp[*it]<now)
45                 {
46                     re.erase(it++);
47                 }
48                 else
49                 it++;
50             }
51             if(re.size()==0){
52                 printf("-1\n");
53                 return 0;
54                 }
55         }
56     }
57     printf("%d\n",*re.begin());
58     return 0;
59 }
60 /*
61 10 6
62 8 5 1 6 6 5 10 6 9 8
63 */

E. Card Game Again
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vova again tries to play some computer card game.

The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number ai is written on the i-th card in the deck.

After receiving the deck and the magic number, Vova removes x (possibly x = 0) cards from the top of the deck, y (possibly y = 0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards x + 1, x + 2, ... n - y - 1, n - y from the original deck.

Vova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid?

Input

The first line contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the numbers written on the cards.

Output

Print the number of ways to choose x and y so the resulting deck is valid.

Examples
Input
3 46 2 8
Output
4
Input
3 69 1 14
Output
1
Note

In the first example the possible values of x and y are:

1. x = 0, y = 0;
2. x = 1, y = 0;
3. x = 2, y = 0;
4. x = 0, y = 1.

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<map>
7 #include<queue>
8 #include<stack>
9 #include<vector>
10 #include<set>
11 #define ll __int64
12 #define mod 100000000
13 using namespace std;
14 ll n,k;
15 ll p;
16 ll num;
17 ll numci;
18 ll sum;
19 int jishu=0;
20 bool check(int x,int y)
21 {
22     for(int i=0;i<jishu;i++){
23        ll zha=sum[y][i]-sum[x-1][i];
24         if(zha<numci[i])
25             return false;
26     }
27     return true;
28 }
29 int main()
30 {
31     scanf("%I64d %I64d",&n,&k);
32     ll exm=k;
33
34     for(ll i=2; i*i<=exm; i++)
35     {
36         if(exm%i==0)
37         {
38             int now=0;
39             num[jishu++]=i;
40             while(exm%i==0){
41                 exm/=i;
42                 now++;
43                 }
44             numci[jishu-1]=now;
45         }
46     }
47     if(exm>1){
48         num[jishu++]=exm;
49         numci[jishu-1]=1;
50         }
51     for(int i=0;i<jishu;i++)
52         sum[i]=0;
53     for(int j=1; j<=n; j++)
54     {
55         scanf("%I64d",&p);
56         for(int k=0;k<jishu;k++){
57             sum[j][k]=sum[j-1][k];
58             while(p%num[k]==0){
59                sum[j][k]++;
60                p/=num[k];
61                }
62             }
63     }
64     ll ans=0;
65     for(int i=1;i<=n;i++)
66     {
67         int l=i,r=n,mid;
68         while(l<r)
69         {
70             mid=(l+r)/2;
71             if(check(i,mid))
72                 r=mid;
73             else
74                 l=mid+1;
75         }
76         if(check(i,r))
77             ans=ans+n-r+1;
78     }
79    printf("%I64d\n",ans);
80     return 0;
81 }