1. 程式人生 > >Codeforces Round #515 (Div. 3) A、B、C、D

Codeforces Round #515 (Div. 3) A、B、C、D

A. Vova and Train

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vova plans to go to the conference by train. Initially, the train is at the point 11 and the destination point of the path is the point LL . The speed of the train is 11 length unit per minute (i.e. at the first minute the train is at the point 11 , at the second minute — at the point 22 and so on).

There are lanterns on the path. They are placed at the points with coordinates divisible by vv (i.e. the first lantern is at the point vv , the second is at the point 2v2v and so on).

There is also exactly one standing train which occupies all the points from ll to rr inclusive.

Vova can see the lantern at the point pp if pp is divisible by vv and there is no standing train at this position (p∉[l;r]p∉[l;r] ). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.

Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to tt different conferences, so you should answer tt independent queries.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104 ) — the number of queries.

Then tt lines follow. The ii -th line contains four integers Li,vi,li,riLi,vi,li,ri (1≤L,v≤1091≤L,v≤109 , 1≤l≤r≤L1≤l≤r≤L ) — destination point of the ii -th path, the period of the lantern appearance and the segment occupied by the standing train.

Output

Print tt lines. The ii -th line should contain one integer — the answer for the ii -th query.

Example

Input

Copy

4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000

Output

Copy

3
0
1134
0

Note

For the first example query, the answer is 33 . There are lanterns at positions 22 , 44 , 66 , 88 and 1010 , but Vova didn't see the lanterns at positions 44 and 66 because of the standing train.

For the second example query, the answer is 00 because the only lantern is at the point 5151 and there is also a standing train at this point.

For the third example query, the answer is 11341134 because there are 12341234 lanterns, but Vova didn't see the lanterns from the position 100100 to the position 199199 inclusive.

For the fourth example query, the answer is 00 because the standing train covers the whole path.

思路:

先說下題意吧,我的理解是給你一個L節車廂的火車,每隔v會有一個燈塔,我們可以看到車廂數字是v的倍數(燈塔把車廂照亮了),不過中間會有一段車廂從l到r由於被別的東西擋住了,即便是燈塔也照不到,問你整列火車能看到的車廂數目

只要讀懂了題很容易就能看出來答案就是總得能看到的減去中間擋住的,即L  / v - (r / v - ( l - 1 ) / v )

程式碼:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    scanf("%d",&t);
    while (t --)
    {
        int a,b,c,d;
        scanf("%d %d %d %d",&a,&b,&c,&d);
        int ans = a / b - (d / b - (c - 1) / b);
        printf("%d\n",ans);
    }
    return 0;
}

B. Heaters

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The ii -th element of the array is 11 if there is a heater in the position ii , otherwise the ii -th element of the array is 00 .

Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position pospos can warm up all the elements in range [pos−r+1;pos+r−1][pos−r+1;pos+r−1] .

Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.

Vova's target is to warm up the whole house (all the elements of the array), i.e. if n=6n=6 , r=2r=2 and heaters are at positions 22 and 55 , then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 elements will be warmed up by the second heater).

Initially, all the heaters are off.

But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.

Your task is to find this number of heaters or say that it is impossible to warm up the whole house.

Input

The first line of the input contains two integers nn and rr (1≤n,r≤10001≤n,r≤1000 ) — the number of elements in the array and the value of heaters.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1 ) — the Vova's house description.

Output

Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

Examples

Input

Copy

6 2
0 1 1 0 0 1

Output

Copy

3

Input

Copy

5 3
1 0 0 0 1

Output

Copy

2

Input

Copy

5 10
0 0 0 0 0

Output

Copy

-1

Input

Copy

10 3
0 0 1 1 0 1 0 0 0 1

Output

Copy

3

Note

In the first example the heater at the position 22 warms up elements [1;3][1;3] , the heater at the position 33 warms up elements [2,4][2,4] and the heater at the position 66 warms up elements [5;6][5;6] so the answer is 33 .

In the second example the heater at the position 11 warms up elements [1;3][1;3] and the heater at the position 55 warms up elements [3;5][3;5] so the answer is 22 .

In the third example there are no heaters so the answer is -1.

In the fourth example the heater at the position 33 warms up elements [1;5][1;5] , the heater at the position 66 warms up elements [4;8][4;8] and the heater at the position 1010 warms up elements [8;10][8;10] so the answer is 33 .

 

思路:

題意很簡單,問能覆蓋全部地板的最少的熱水器的個數,

貪心,每次找到能覆蓋上一次覆蓋位置的最遠的熱水器的位置找不到則覆蓋不了全部地板,輸出-1

程式碼:

#include<bits/stdc++.h>
using namespace std;
int a[1010];
int main()
{
    int n,r,ans = 0,t = 0;
    scanf("%d %d",&n,&r);
    for (int i = 0;i < n;i ++) scanf("%d",&a[i]);
    while (t < n)
    {
        int idx = -1;
        for (int i = 0;i < n;i ++)
            if (a[i])
                if (i - r + 1 <= t && i + r - 1 >= t)
                    idx = i;//能覆蓋上一次覆蓋位置的最遠的熱水器的位置
        if (idx == -1)
        {
            puts("-1\n");
            return 0;
        }
        ans ++;
        t = idx + r;//當前的最遠覆蓋位置
    }
    printf("%d\n",ans);
    return 0;
}

C. Books Queries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have got a shelf and want to put some books on it.

You are given qq queries of three types:

  1. L idid — put a book having index idid on the shelf to the left from the leftmost existing book;
  2. R idid — put a book having index idid on the shelf to the right from the rightmost existing book;
  3. ? idid — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index idid will be leftmost or rightmost.

You can assume that the first book you will put can have any position (it does not matter) and queries of type 33 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don't put the same book on the shelf twice, so idid s don't repeat in queries of first two types.

Your problem is to answer all the queries of type 33 in order they appear in the input.

Note that after answering the query of type 33 all the books remain on the shelf and the relative order of books does not change.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105 ) — the number of queries.

Then qq lines follow. The ii -th line contains the ii -th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 33 , it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).

It is guaranteed that there is at least one query of type 33 in the input.

In each query the constraint 1≤id≤2⋅1051≤id≤2⋅105 is met.

Output

Print answers to queries of the type 33 in order they appear in the input.

Examples

Input

Copy

8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1

Output

Copy

1
1
2

Input

Copy

10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115

Output

Copy

0
2
1

Note

Let's take a look at the first example and let's consider queries:

  1. The shelf will look like [1][1] ;
  2. The shelf will look like [1,2][1,2] ;
  3. The shelf will look like [1,2,3][1,2,3] ;
  4. The shelf looks like [1,2,3][1,2,3] so the answer is 11 ;
  5. The shelf will look like [4,1,2,3][4,1,2,3] ;
  6. The shelf looks like [4,1,2,3][4,1,2,3] so the answer is 11 ;
  7. The shelf will look like [5,4,1,2,3][5,4,1,2,3] ;
  8. The shelf looks like [5,4,1,2,3][5,4,1,2,3] so the answer is 22 .

Let's take a look at the second example and let's consider queries:

  1. The shelf will look like [100][100] ;
  2. The shelf will look like [100,100000][100,100000] ;
  3. The shelf will look like [100,100000,123][100,100000,123] ;
  4. The shelf will look like [101,100,100000,123][101,100,100000,123] ;
  5. The shelf looks like [101,100,100000,123][101,100,100000,123] so the answer is 00 ;
  6. The shelf will look like [10,101,100,100000,123][10,101,100,100000,123] ;
  7. The shelf will look like [10,101,100,100000,123,115][10,101,100,100000,123,115] ;
  8. The shelf looks like [10,101,100,100000,123,115][10,101,100,100000,123,115] so the answer is 22 ;
  9. The shelf will look like [10,101,100,100000,123,115,110][10,101,100,100000,123,115,110] ;
  10. The shelf looks like [10,101,100,100000,123,115,110][10,101,100,100000,123,115,110] so the answer is 11 .

思路:

開兩個陣列存左右(注意每次詢問的書並沒有被拿出來,這樣就很簡單了)

程式碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = (int)2e5 + 10;
int a[maxn],b[maxn];
int main()
{
    int n,l = 0,r = 0,x;
    char q[3];
    scanf("%d",&n);
    while (n --)
    {
        scanf("%s %d",q,&x);
        if (q[0] == 'L')
            a[x] = ++ l;
        else if (q[0] == 'R')
            b[x] = ++ r;
        else
        {
            if (a[x])
                printf("%d\n",min(l - a[x],r + a[x] - 1));
            else
                printf("%d\n",min(r - b[x],l + b[x] - 1));
        }
    }
    return 0;
}

D. Boxes Packing

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Maksim has nn objects and mm boxes, each box has size exactly kk . Objects are numbered from 11 to nn in order from left to right, the size of the ii -th object is aiai .

Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the ii -th object fits in the current box (the remaining size of the box is greater than or equal to aiai ), he puts it in the box, and the remaining size of the box decreases by aiai . Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects.

Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has.

Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change).

Input

The first line of the input contains three integers nn , mm , kk (1≤n,m≤2⋅1051≤n,m≤2⋅105 , 1≤k≤1091≤k≤109 ) — the number of objects, the number of boxes and the size of each box.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k ), where aiai is the size of the ii -th object.

Output

Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement.

Examples

Input

Copy

5 2 6
5 2 1 4 2

Output

Copy

4

Input

Copy

5 1 4
4 2 3 4 1

Output

Copy

1

Input

Copy

5 3 3
1 2 3 1 1

Output

Copy

5

Note

In the first example Maksim can pack only 44 objects. Firstly, he tries to pack all the 55 objects. Distribution of objects will be [5],[2,1][5],[2,1] . Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be [2,1],[4,2][2,1],[4,2] . So the answer is 44 .

In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is [4][4] ), but he can pack the last object ([1][1] ).

In the third example Maksim can pack all the objects he has. The distribution will be [1,2],[3],[1,1][1,2],[3],[1,1] .

思路:

我們直接從右邊開始放,反正這樣做的沒有人被hack

程式碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = (int)2e5 + 10;
int a[maxn];
int main()
{
    int n,m,k;
    scanf("%d %d %d",&n,&m,&k);
    for (int i = 1;i <= n;i ++)
        scanf("%d",&a[i]);
    int ans = 0,i = n,rest = k;
    while (i > 0 && m > 0)
    {
        if (a[i] <= rest)
        {
            rest -= a[i];
            ans ++;
            i --;
        }
        else
        {
            if (a[i]> k)
                break;
            m --;
            rest = k;
        }
    }
    printf("%d\n",ans);
    return 0;
}

E. Binary Numbers AND Sum

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two huge binary integer numbers aa and bb of lengths nn and mm respectively. You will repeat the following process: if b>0b>0 , then add to the answer the value a & ba & b and divide bb by 22 rounding down (i.e. remove the last digit of bb ), and repeat the process again, otherwise stop the process.

The value a & ba & b means bitwise AND of aa and bb . Your task is to calculate the answer modulo 998244353998244353 .

Note that you should add the value a & ba & b to the answer in decimal notation, not in binary. So your task is to calculate the answer in decimal notation. For example, if a=10102 (1010)a=10102 (1010) and b=10002 (810)b=10002 (810) , then the value a & ba & b will be equal to 88 , not to 10001000 .

Input

The first line of the input contains two integers nn and mm (1≤n,m≤2⋅1051≤n,m≤2⋅105 ) — the length of aa and the length of bb correspondingly.

The second line of the input contains one huge integer aa . It is guaranteed that this number consists of exactly nn zeroes and ones and the first digit is always 11 .

The third line of the input contains one huge integer bb . It is guaranteed that this number consists of exactly mm zeroes and ones and the first digit is always 11 .

Output

Print the answer to this problem in decimal notation modulo 998244353998244353 .

Examples

Input

Copy

4 4
1010
1101

Output

Copy

12

Input

Copy

4 5
1001
10101

Output

Copy

11

Note

The algorithm for the first example:

  1. add to the answer 10102 & 11012=10002=81010102 & 11012=10002=810 and set b:=110b:=110 ;
  2. add to the answer 10102 & 1102=102=21010102 & 1102=102=210 and set b:=11b:=11 ;
  3. add to the answer 10102 & 112=102=21010102 & 112=102=210 and set b:=1b:=1 ;
  4. add to the answer 10102 & 12=02=01010102 & 12=02=010 and set b:=0b:=0 .

So the answer is 8+2+2+0=128+2+2+0=12 .

The algorithm for the second example:

  1. add to the answer 10012 & 101012=12=11010012 & 101012=12=110 and set b:=1010b:=1010 ;
  2. add to the answer 10012 & 10102=10002=81010012 & 10102=10002=810 and set b:=101b:=101 ;
  3. add to the answer 10012 & 1012=12=11010012 & 1012=12=110 and set b:=10b:=10 ;
  4. add to the answer 10012 & 102=02=01010012 & 102=02=010 and set b:=1b:=1 ;
  5. add to the answer 10012 & 12=12=11010012 & 12=12=110 and set b:=0b:=0 .

So the answer is 1+8+1+0+1=111+8+1+0+1=11 .

思路:

對於b串中的1,那麼a串中的對應位置後的每個1都會得到一個結果,於是我們只要先處理出a串中的字首和,再暴力處理即可

程式碼:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 998244353;
const int maxn = (int)2e5 + 10;
char s1[maxn],s2[maxn];
ll ans[maxn];
ll quick(ll a,ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
    scanf("%s %s",s1,s2);
    int idx = 0;
    for (int i = n - 1;i >= 0;i --)
    {
        if (s1[i] == '1')
            ans[i] = (quick(2,idx) + ans[i + 1]) % mod;
        else
            ans[i] = ans[i + 1];
        idx ++;
    }
    ll res = 0;
    if (n < m)
    {
        for (int i = 0;i < m- n;i ++)
            if (s2[i] == '1')
                res = (res + ans[0]) % mod;
        for (int i =  m - n;i < m;i ++)
        {
            if (s2[i] == '1')
                res = (res + ans[i - m + n]) % mod;
        }
    }
    else
    {
        for (int i = 0;i < m;i ++)
            if (s2[i] == '1')
                res = (res + ans[i + n - m]) % mod;
    }
    printf("%lld\n",res);
    return 0;
}