Codeforces Round #786 (Div. 3) A~E

目录

A. Number Transformation

B. Dictionary

C. Infinite Replacement

D. A-B-C Sort

E. Breaking the Wall

———————————————————————————————————————————

A. Number Transformation

You are given two integers xx and yy. You want to choose two strictly positive (greater than zero) integers aa and bb, and then apply the following operation to xx exactly aa times: replace xx with b⋅xb⋅x.

You want to find two positive integers aa and bb such that xx becomes equal to yy after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.

For example:

  • if x=3x=3 and y=75y=75, you may choose a=2a=2 and b=5b=5, so that xx becomes equal to 3⋅5⋅5=753⋅5⋅5=75;
  • if x=100x=100 and y=100y=100, you may choose a=3a=3 and b=1b=1, so that xx becomes equal to 100⋅1⋅1⋅1=100100⋅1⋅1⋅1=100;
  • if x=42x=42 and y=13y=13, there is no answer since you cannot decrease xx with the given operations.

Input

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

Each test case consists of one line containing two integers xx and yy (1≤x,y≤1001≤x,y≤100).

Output

If it is possible to choose a pair of positive integers aa and bb so that xx becomes yy after the aforementioned process, print these two integers. The integers you print should be not less than 11 and not greater than 109109 (it can be shown that if the answer exists, there is a pair of integers aa and bb meeting these constraints). If there are multiple such pairs, print any of them.

If it is impossible to choose a pair of integers aa and bb so that xx becomes yy, print the integer 00 twice.

Example

input

3

3 75

100 100

42 13

output

2 5

3 1

0 0

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
const int N = 2e5 + 10;
const long long mod = 998244353 ;


typedef long long ll;
typedef pair<int, int> PII;

#define forn(i,a,b) for(int i=a;i<b;i++)
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define pb push_back

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

int x,y,t;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>x>>y;
        if(x>y) cout<<"0 0"<<endl; 
        else if(y%x==0) 
            cout<<1<<" "<<y/x<<endl;
        else cout<<"0 0"<<endl;
    }
	return  0;
}

B. Dictionary

The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language.

The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word aa comes earlier than word bb in the dictionary if one of the following conditions hold:

  • the first letter of aa is less than the first letter of bb;
  • the first letters of aa and bb are the same, and the second letter of aa is less than the second letter of bb.

So, the dictionary looks like that:

  • Word 11: ab
  • Word 22: ac
  • ...
  • Word 2525: az
  • Word 2626: ba
  • Word 2727: bc
  • ...
  • Word 649649: zx
  • Word 650650: zy

You are given a word ss from the Berland language. Your task is to find its index in the dictionary.

Input

The first line contains one integer tt (1≤t≤6501≤t≤650) — the number of test cases.

Each test case consists of one line containing ss — a string consisting of exactly two different lowercase Latin letters (i. e. a correct word of the Berland language).

Output

For each test case, print one integer — the index of the word ss in the dictionary.

Example

input

7
ab
ac
az
ba
bc
zx
zy

output

1
2
25
26
27
649
650

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
const int N = 2e5 + 10;
const long long mod = 998244353 ;


typedef long long ll;
typedef pair<int, int> PII;

#define forn(i,a,b) for(int i=a;i<b;i++)
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define pb push_back

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

int t;
string s;
unordered_map<string,int> h;
void init()
{
    string s="ab";
    int m=1;
    h.insert({s,m});
    while(s[0]!='z'||s[1]!='z')
    {
        if(s[1]=='z')
        {
            s[0]+=1;
            s[1]='a';
        }
        else s[1]++;
        if(s[0]==s[1]) continue;
        m++;
        h.insert({s,m});
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
    cin>>t;
    init();  
    while(t--)
    {
        cin>>s;
        cout<<h[s]<<endl;
    }
	return 0;
}

C. Infinite Replacement

You are given a string ss, consisting only of Latin letters 'a', and a string tt, consisting of lowercase Latin letters.

In one move, you can replace any letter 'a' in the string ss with a string tt. Note that after the replacement string ss might contain letters other than 'a'.

You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.

Two strings are considered different if they have different length, or they differ at some index.

Input

The first line contains a single integer qq (1≤q≤1041≤q≤104) — the number of testcases.

The first line of each testcase contains a non-empty string ss, consisting only of Latin letters 'a'. The length of ss doesn't exceed 5050.

The second line contains a non-empty string tt, consisting of lowercase Latin letters. The length of tt doesn't exceed 5050.

Output

For each testcase, print the number of different strings ss that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number

Example

input

3

aaaa

a

aa

abc

a

b

output

1

-1

2

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
const int N = 2e5 + 10;
const long long mod = 998244353 ;


typedef long long ll;
typedef pair<int, int> PII;

#define forn(i,a,b) for(int i=a;i<b;i++)
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define pb push_back

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

int n;
string t,s;

void solve()
{
    cin>>s>>t;
    if(t=="a") 
    {
        cout<<1<<endl;
        return ;
    }
    if(count(t.begin(),t.end(),'a')>0) 
    {
        cout<<-1<<endl;
        return;
    }
    cout<<(1LL<<s.size())<<endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
    cin>>n;
    while(n--)
    {
        solve();
    }
	return 0;
}

D. A-B-C Sort

You are given three arrays aa, bb and cc. Initially, array aa consists of nn elements, arrays bb and cc are empty.

You are performing the following algorithm that consists of two steps:

  • Step 11: while aa is not empty, you take the last element from aa and move it in the middle of array bb. If bb currently has odd length, you can choose: place the element from aa to the left or to the right of the middle element of bb. As a result, aa becomes empty and bb consists of nn elements.
  • Step 22: while bb is not empty, you take the middle element from bb and move it to the end of array cc. If bb currently has even length, you can choose which of two middle elements to take. As a result, bb becomes empty and cc now consists of nn elements.

Refer to the Note section for examples.

Can you make array cc sorted in non-decreasing order?

Input

The first line contains a single integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Next tt cases follow.

The first line of each test case contains the single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — the array aa itself.

It's guaranteed that the sum of nn doesn't exceed 2⋅1052⋅105.

Output

For each test, print YES (case-insensitive), if you can make array cc sorted in non-decreasing order. Otherwise, print NO (case-insensitive).

Example

input

3

4

3 1 5 3

3

3 2 1

1

7331

output

YES

NO

YES

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
const int N = 2e5 + 10;
const long long mod = 998244353 ;


typedef long long ll;
typedef pair<int, int> PII;

#define forn(i,a,b) for(int i=a;i<b;i++)
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define pb push_back

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

int t;
int n,a[N];

void solve()
{
    cin>>n;
    vector<int> b(n);
    for(int i=0;i<n;i++) cin>>b[i];
    for(int i=n-2;i>=0;i-=2)
    {
        if(b[i]>b[i+1]) swap(b[i],b[i+1]);
    }
    if(is_sorted(b.begin(),b.end())) cout<<"YES\n";
    else cout<<"NO\n";
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
    cin>>t;
    while(t--)
    {
        solve();
    }
	return 0;
}

E. Breaking the Wall

Monocarp plays "Rage of Empires II: Definitive Edition" — a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall.

The wall consists of nn sections, aligned in a row. The ii-th section initially has durability aiai. If durability of some section becomes 00 or less, this section is considered broken.

To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals 22 damage to the target section and 11 damage to adjacent sections. In other words, if the onager shoots at the section xx, then the durability of the section xx decreases by 22, and the durability of the sections x−1x−1 and x+1x+1 (if they exist) decreases by 11 each.

Monocarp can shoot at any sections any number of times, he can even shoot at broken sections.

Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him!

Input

The first line contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of sections.

The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106), where aiai is the initial durability of the ii-th section.

Output

Print one integer — the minimum number of onager shots needed to break at least two sections of the wall.

Example

input

5

20 10 30 10 20

output

10

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;
const int N = 2e5 + 10;
const long long mod = 998244353 ;
const int INF = 0x3f3f3f3f;

typedef long long ll;
typedef pair<int, int> PII;

#define forn(i,a,b) for(int i=a;i<b;i++)
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define pb push_back

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}

int t;
int a[N];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin>>t;
    int minv1=INF,minv2=INF;
    for(int i=1;i<=t;i++)
    {
        cin>>a[i];
        if(a[i]<=minv1) minv2=minv1,minv1=a[i];
        else if(a[i]<minv2) minv2=a[i];
    }
    int res=(minv1+1>>1)+(minv2+1>>1);
    for(int i=1;i+2<=t;i++)
        res=min(res,min(a[i],a[i+2])+(abs(a[i]-a[i+2])+1>>1));
	for(int i=1;i+1<=t;i++){
        int t1=a[i],t2=a[i+1];
        if(t1>t2) swap(t1,t2);
        if(t2>2*t1) res=min(res,t2+1>>1);
        else res=min(res,(t1+t2+2)/3);
    }
    cout<<res<<endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'<王半仙>'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值