思维题练习

A - Shell Game

 

Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.

Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).

Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?

Input

The first line of the input contains an integer n(1 ≤ n ≤ 2·109) — the number of movements made by the operator.

The second line contains a single integer x (0 ≤ x ≤ 2) — the index of the shell where the ball was found after n movements.

Output

Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.

Examples

Input

4
2

Output

1

Input

1
1

Output

0

Note

In the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements.

  1. During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell.
  2. During the second move operator swapped the middle shell and the right one. The ball is still under the left shell.
  3. During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle.
  4. Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell.

这个题其实就是 找规律打表  

奇数步左边和中间的交换,偶数步中间的和右边的交换。多进行几步 就可以发现

刚开始 0,1,2
第一步 1,0 2
第二步 1,2,0,
第三步 2,1,0,
第四步 2,0,1,
第五步 0,2,1

第六步 0,1,2又回到了刚开始的状态,所以移动规律就是6步一个循环,我们可以打表记录一下这些位置状态

所以进行步数n后在第x个位置的小球,最开始位置就是a[n%6][x]

#include<bits/stdc++.h>
using namespace std;
int a[6][3]=
{
0,1,2,
1,0,2,
1,2,0,
2,1,0,
2,0,1,
0,2,1,
};
long long n,x;
int main()
{
    cin>>n>>x;
    cout<<a[n%6][x]<<endl;
}

B - Game of Credit Cards

 

After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.

Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.

Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.

Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.

The second line contains n digits — Sherlock's credit card number.

The third line contains n digits — Moriarty's credit card number.

Output

First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.

Examples

Input

3
123
321

Output

0
2

Input

2
88
00

Output

2
0

Note

First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.

这个题感觉类似田忌赛马,知道别人的出马顺序,然后安排自己的出马顺序。

如果想自己输得次数最少,那也就是自己赢或者平局的次数最多。可以贪心来做,对方出一个数,那我们就从所有的数里找出大于或者等于这个数的最小的数来抗衡他,为什么是最小的数呢?因为后边他可能出比这个大的数,我们才有机会用大的数来抗衡它。例如 他有1 2 3 我们有3 2 1,如果他出2,那我们出从所有的数里找出大于或者等于这个数的最小的数也就是2,因为我们要留着3去抵抗他的3 这样才能输的最少。那如何找 所有的数里大于或者等于这个数的最小的数呢?那我们就要从大到小排序。

如果想自己赢得最多,也就是上边的情况去掉平局。同理来做

#include<bits/stdc++.h>
using namespace std;
char a[1000+5],b[1000+5];
int aa[1000+5],bb[1000+5];
int bbb[1000+5];
int main()
{
    int n;
    cin>>n;
    cin>>a>>b;
    for(int i=0;i<n;i++)
    {
        aa[i]=a[i]-'0';
        bb[i]=b[i]-'0';
        bbb[i]=bb[i];
    }
    sort(aa,aa+n,greater<int>());
    sort(bb,bb+n,greater<int>());

    int Count1=0;//最少
    int Count2=0;//最多

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(bb[j]>=aa[i])
            {
                bb[j]=0;
                Count1++;
                break;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(bbb[j]>aa[i])
            {
                bbb[j]=0;
                Count2++;
                break;
            }
        }
    }

    cout<<n-Count1<<endl<<Count2<<endl;
}

C - Alyona and Spreadsheet

 

During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.

Now she has a table filled with integers. The table consists of n rows and mcolumns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n - 1.

Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to rinclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.

Alyona is too small to deal with this task and asks you to help!

Input

The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.

Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).

The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.

The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).

Output

Print "Yes" to the i-th line of the output if the table consisting of rows from lito ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".

Example

Input

5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5

Output

Yes
No
Yes
Yes
Yes
No

Note

In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.

题意是 给你一个n*m的矩阵,给l,r的范围,如果从第l行到第r行中至少有一列数是非递减就符合要求输出Yes,不符合输出No

那我们可以用一个数组来记录符合要求的 每一行最高到第几行(越高数越小),用一个数组来记录每一列最高到第几行

如果第r行最高可以到的行数小于l就可以到达(越高数越小)

#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
int main()
{
    int a[maxn],b[maxn],c[maxn];//a[i] 每一行的数
                                //b[i]某一行的某一列(最高的一列)能到达的最上方的行
                                //c[i]某一行能到达的行
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
        b[i]=1;
    for(int i=1;i<=n;i++)
    {
        c[i]=i;//这一行能到第几行(每个数都能到达他自己所在的行)  
        for(int j=1;j<=m;j++)
        {
            int x;
            cin>>x;
            if(x<a[j])//非递增 不满足条件
                b[j]=i;//第j列最高到第i行
            a[j]=x;
            if(b[j]<c[i])//如果这一行中的一列 可以到达更高的行
                c[i]=b[j];//最高到第b[j]行
        }
    }
    int k;
    cin>>k;
    while(k--)
    {
        int l,r;
        cin>>l>>r;
        if(c[r]<=l)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}



D - A Serial Killer

 

Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.

The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.

You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.

Input

First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.

Next n lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.

The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.

Output

Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.

Examples

Input

ross rachel
4
ross joey
rachel phoebe
phoebe monica
monica chandler

Output

ross rachel
joey rachel
joey phoebe
joey monica
joey chandler

Input

icm codeforces
1
codeforces technex

Output

icm codeforces
icm technex

Note

In first example, the killer starts with ross and rachel.

  • After day 1, ross is killed and joey appears.
  • After day 2, rachel is killed and phoebe appears.
  • After day 3, phoebe is killed and monica appears.
  • After day 4, monica is killed and chandler appears.

名字替换就可以了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a,b,c,d;
    cin>>a>>b;
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        cout<<a<<' '<<b<<endl;
        cin>>c>>d;//c是被kill的
        if(c==a)//a被kill d替补a 
            a=d;
        else if(c==b)//b被kill d替补b 
            b=d;
        if(i==t-1)
            cout<<a<<' '<<b<<endl;
    }
}

E - Sherlock and his girlfriend

 

Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry.

He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, ... n + 1.

Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don't have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.

Help Sherlock complete this trivial task.

Input

The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.

Output

The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints.

The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price.

If there are multiple ways to color the pieces using k colors, you can output any of them.

Examples

Input

3

Output

2
1 1 2 

Input

4

Output

2
2 1 1 2

Note

In the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.

In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4must be distinct.

有n片贝壳,1.2.3.....n 价格是 2.3.4....n+1; 贝壳要染色 如果一个贝壳的价格是另一个贝壳价格的质因子的话,那么这两个贝壳不能同色。问至少需要颜色数和怎么染色。

最小的质数是2,那么分n>2和n<=2讨论,因为n<=2是,肯定没有质因子,所以一种颜色就够了。

当n>2时,可能有的价格优有质因子,我们可以把其中包括的所有质数都涂成一种颜色,比如都为1色,别的数可以都涂成2色,这样合数和他的质因子数颜色肯定不相同。所以我们需要先筛出素数判断一下。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+5;
bool isprime[maxn];

void Prime(int k)
{
    for(int i=2;i <=k+1;++i)
    {
        if(isprime[i])//判断i是否为素数
        //筛去由已有的素数确定的合数
            for(int j=2;j*i <= k+1;++j)
                isprime[i*j]=false;
    }
}



int main()
{
    memset(isprime,true,sizeof isprime);
    int k;
    cin>>k; //2....k+1
    if(k<=2)
    {
        cout<<1<<endl;
        for(int i=2;i<=k+1;i++)
            cout << 1<<' ';
    }
    else
    {
        cout<<2<<endl;
        Prime(k);
        for(int i=2;i<=k+1;i++)
        {
            if(isprime[i])
                cout<<1<<' ';
            else
                cout<<2<<' ';
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值