QLU—寒假第二次训练

A - Generous Kefa

 

One day Kefa found n baloons. For convenience, we denote color of i-th baloon as si— lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of the same color. Kefa want to give out all baloons to his friends. Help Kefa to find out, can he give out all his baloons, such that no one of his friens will be upset — print «YES», if he can, and «NO», otherwise. Note, that Kefa's friend will not upset, if he doesn't get baloons at all.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 100) — the number of baloons and friends.

Next line contains string s — colors of baloons.

Output

Answer to the task — «YES» or «NO» in a single line.

You can choose the case (lower or upper) for each letter arbitrary.

Examples

Input

4 2
aabb

Output

YES

Input

6 3
aacaab

Output

NO

Note

In the first sample Kefa can give 1-st and 3-rd baloon to the first friend, and 2-nd and 4-th to the second.

In the second sample Kefa needs to give to all his friends baloons of color a, but one baloon will stay, thats why answer is «NO».

题意:分气球,气球的颜色用小写字母来表示,必须保证每个人拿到的气球是不一样颜色的。

水题,只需要保证任何一种颜色的气球的数目不大于k即可。

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int n,k,a[200];
int main()
{
    while(cin>>n>>k){
        char c;
        bool flag=true;
        for(int i=1;i<=n;i++){
            cin>>c;
            a[c-'0']++;
            if(a[c-'0']>k){
                printf("NO\n");
                flag=false;
                break;
            }
        }
        if(flag==true)
            printf("YES\n");
    }
}

B - Godsend(博弈论)

 

Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?

Input

First line of input data contains single integer n (1 ≤ n ≤ 106) — length of the array.

Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

Output

Output answer in single line. "First", if first player wins, and "Second" otherwise (without quotes).

Examples

Input

4
1 3 2 3

Output

First

Input

2
2 2

Output

Second

Note

In first sample first player remove whole array in one move and win.

In second sample first player can't make a move and lose.

题意:由n个数字组成的数组,第一个只能取连续的,加起来为奇数的数字,第二个人只能取连续的,加起来为偶数的数字。

分析题目:如果这些数字的和为奇数,那么第一个人可以直接将所有的都取走,那么第一个获胜;

如果这些数字全部为偶数,还是第一个人先取,只要这些数中有一个奇数,第一个人取走,剩下的数的和还是奇数,第二个就算取了一段偶数,那么第一个人再取一次就可以全部取走。

所以综上分析,只要这n个数中有一个数是奇数,那么第一个人赢,否则第二个人赢。

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int n;
int main()
{
    while(~sc(n)){
        int a;
        bool flag=true;
        for(int i=1;i<=n;i++){
            sc(a);
            if(a&1)
                flag=false;
        }
        if(flag==true)
            printf("Second\n");
            else
                printf("First\n");
    }
}

C - Leha and Function(补)  

Leha like all kinds of strange things. Recently he liked the function F(n, k). Consider all possible k-element subsets of the set [1, 2, ..., n]. For subset find minimal element in it. F(n, k) — mathematical expectation of the minimal element among all k-element subsets.

But only function does not interest him. He wants to do interesting things with it. Mom brought him two arrays A and B, each consists of m integers. For all i, j such that 1 ≤ i, j ≤ m the condition Ai ≥ Bj holds. Help Leha rearrange the numbers in the array A so that the sum  is maximally possible, where A' is already rearranged array.

Input

First line of input data contains single integer m (1 ≤ m ≤ 2·105) — length of arrays A and B.

Next line contains m integers a1, a2, ..., am (1 ≤ ai ≤ 109) — array A.

Next line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 109) — array B.

Output

Output m integers a'1, a'2, ..., a'm — array A' which is permutation of the array A.

Examples

Input

5
7 3 5 3 4
2 1 3 2 3

Output

4 7 3 5 3

Input

7
4 6 5 8 8 2 6
2 1 2 2 1 1 2

Output

2 6 4 5 8 8 6

题意:自我感觉这个题目不大好懂,所谓F(n,k)就是在1,2...n中所有的k个的组合中找最小的取平均值,

举个栗子:

F(5,2)=(1,2)(1,3)(1,4)(1,5)(2,3)(2,4)(2,5)(3,4)(3,5)(4,5),这10个组合中取最小的也就是1,1,1,1,2,2,2,3,3,4,求平均值,(1+1+1+1+2+2+2+3+3+4)/10=2.

然后给两个数组,A,B,求怎样把A数组变换位置使sigmaF(A',B)最大。

根据上面这个例子我们可以发现这样一个规律,F(n,k)中,n越大也就意味着分子上的数越多值也就越大,k越小也就意味着组合的数目越少值也就越小,也就是等价于让A越大的同时B越小

当然这个规律我没办法验证......大家也可以举几个例子看看,像F(4,1),F(4,2),F(5,2),试试就可以发现这个规律

剩下的求解也就简单了,将A从大到小排序,B从小到大排序,将B离散化即可

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=2e5+10;
int n,a[N],ran[N];
struct node
{
    int a;
    int p;
}b[N];
bool cmp(const int a,const int b)
{
    return a>b;
}
bool cmp1(node c,node b)
{
    return c.a<b.a;
}
int main()
{
    while(~sc(n)){
        for(int i=1;i<=n;i++)
            sc(a[i]);
        for(int i=1;i<=n;i++){
            sc(b[i].a);
            b[i].p=i;
        }
        sort(a+1,a+n+1,cmp);
        sort(b+1,b+n+1,cmp1);
        for(int i=1;i<=n;i++)
        ran[b[i].p]=i;             //离散化的过程
        for(int i=1;i<=n;i++){
            printf("%d",a[ran[i]]);
            if(i!=n)
                printf(" ");
        }
        printf("\n");
    }
}

F - New Year and Days

Today is Wednesday, the third day of the week. What's more interesting is that tomorrow is the last day of the year 2015.

Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016.

Limak wants to prove how responsible a bear he is. He is going to regularly save candies for the entire year 2016! He considers various saving plans. He can save one candy either on some fixed day of the week or on some fixed day of the month.

Limak chose one particular plan. He isn't sure how many candies he will save in the 2016 with his plan. Please, calculate it and tell him.

Input

The only line of the input is in one of the following two formats:

  • "x of week" where x (1 ≤ x ≤ 7) denotes the day of the week. The 1-st day is Monday and the 7-th one is Sunday.
  • "x of month" where x (1 ≤ x ≤ 31) denotes the day of the month.

Output

Print one integer — the number of candies Limak will save in the year 2016.

Examples

Input

4 of week

Output

52

Input

30 of month

Output

11

Note

Polar bears use the Gregorian calendar. It is the most common calendar and you likely use it too. You can read about it on Wikipedia if you want to – https://en.wikipedia.org/wiki/Gregorian_calendar. The week starts with Monday.

In the first sample Limak wants to save one candy on each Thursday (the 4-th day of the week). There are 52 Thursdays in the 2016. Thus, he will save 52 candies in total.

In the second sample Limak wants to save one candy on the 30-th day of each month. There is the 30-th day in exactly 11 months in the 2016 — all months but February. It means that Limak will save 11 candies in total.

 题意:一只小北极熊要在2016年攒糖果,它可以选择每周攒一次,可以是周一到周日的任何一天攒,或者每月攒一次,也可以是每个月的任何一天攒。所以就是求可以攒多少个?

题目说这个北极熊用的是一个 Gregorian日历,我是直接按正常日历算的......

2016是闰年,所以有366天,因为2016年的第一天是周五,所以366/7=52---2,所以除了周五周六有53个,其他的都只有52个。

再看月份的,2月有29天,所以1号到29号都有12个,30号11个,31号只有7个。

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int n;
char s1[10],s2[10];
int main()
{
    while(~scanf("%d%s%s",&n,s1,s2)){
        if(s2[0]=='w'){
            if(n==5||n==6)
                printf("53\n");
            else printf("52\n");
        }
        else {
            if(n==30)
                printf("11\n");
            else if(n==31)
                printf("7\n");
            else printf("12\n");
        }
    }
}

G - New Year and Domino (补)

They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so.

Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1through w from left to right.

Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid.

Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle?

Input

The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the number of rows and the number of columns, respectively.

The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively.

The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries.

Each of the next q lines contains four integers r1ic1ir2ic2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.

Output

Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.

Examples

input

Copy

5 8
....#..#
.#......
##.#....
##..#.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8

output

Copy

4
0
10
15

input

Copy

7 39
.......................................
.###..###..#..###.....###..###..#..###.
...#..#.#..#..#.........#..#.#..#..#...
.###..#.#..#..###.....###..#.#..#..###.
.#....#.#..#....#.....#....#.#..#..#.#.
.###..###..#..###.....###..###..#..###.
.......................................
6
1 1 3 20
2 10 6 30
2 10 7 30
2 2 7 7
1 7 7 7
1 8 7 8

output

Copy

53
89
120
23
0
2

Note

A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.

题意: 在给定的n×m的方格中,摆多米诺骨牌,一块多米诺骨牌占两个位置,可以竖着也可以横着,给q个询问,两个坐标构成的矩形,问这个矩形内可以有多少种摆法?

很明显用二维前缀和,这跟我之前做过的一个摆战舰的题感觉很像,也是在一个方格中问有多少种战舰的摆法,战舰占几个格子和怎么摆忘了。

那具体这个题可以分别两个二维数组存横着摆和竖着摆的摆法数量,先预处理,在相减就可以了。

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int a[510][510],b[510][510],n,m,q;
char c[510][510];
int main()
{
    while(~scanf("%d%d",&n,&m)){
        met(a,0);
        met(b,0);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            cin>>c[i][j];
            for(int i=1;i<=n;i++)
            for(int j=1;j<m;j++){
                if(c[i][j]==c[i][j+1]&&c[i][j]=='.')
                    a[i][j]++;
            }
            for(int i=1;i<=m;i++)
            for(int j=1;j<n;j++){
                if(c[j][i]==c[j+1][i]&&c[j][i]=='.')
                    b[j][i]++;
            }
            for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)   
                a[i][j]+=a[i][j-1];    //求前缀和
            for(int i=1;i<=m;i++)
                for(int j=1;j<=n;j++)
                b[j][i]+=b[j-1][i];       //求前缀和
            sc(q);
            int r1,c1,r2,c2;
            while(q--){
                int ans=0;
                scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
                for(int i=r1;i<=r2;i++)
                    ans+=a[i][c2-1]-a[i][c1-1];   //这里-1是因为前面求的时候是当前格子和后一个格子判断
                for(int i=c1;i<=c2;i++)
                    ans+=b[r2-1][i]-b[r1-1][i];
                printf("%d\n",ans);
            }
    }
}

H - New Year and Old Property

The year 2015 is almost over.

Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.

Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?

Assume that all positive integers are always written without leading zeros.

Input

The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.

Output

Print one integer – the number of years Limak will count in his chosen interval.

Examples

Input

5 10

Output

2

Input

2015 2015

Output

1

Input

100 105

Output

0

Input

72057594000000000 72057595000000000

Output

26

Note

In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property.

 题意:这个题目意思很好懂,就是给定一个范围,在这个范围内的十进制数转化为二进制数中,二进制里0的个数为1的有多少个

虽然题意好懂,但是我对着题目懵逼了好长时间,本来想用前缀和做,发现10^18次方,数组貌似存不下。。。

最后找了老半天规律才有了些思路

因为要0的个数只有一个的,所以从1开始找,1乘上2,二进制就从1变成了10,这样就符合要求了,10如果想要继续找下去,10<<1,成了100,所以要加上1,就是101,所以继续*2+1即可,那这只是0在第二个位置上,如果在第三个位置呢?根据上面的描述就可以想到1先*2+1,成了11,在*2即可。

于是就有了一个规律,如果一个数不符合规律,*2即可使它符合规律,*2+1则不符合规律,

如果一个数符合规律,那么如果想要继续找下去只能*2+1,

这其实算是一个深搜的过程,不断找下去,如果这个数在区间内计数器ans就+1,直到这个数大于区间的最大值为止。

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
ll a,b,ans;
void dfs(ll x,bool y)
{
    if(x>b)
        return ;
    if(x>=a&&x<=b&&y==1)
            ans++;
    if(!y){
        dfs(2*x,1);
        dfs(2*x+1,0);
    }
    else
        dfs(2*x+1,1);
}
int main()
{
    while(~scanf("%lld%lld",&a,&b)){
        ans=0;
        dfs(1,0);
        printf("%lld\n",ans);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值