1. 程式人生 > >Educational Codeforces Round 57 (Rated for Div. 2) (待更新)

Educational Codeforces Round 57 (Rated for Div. 2) (待更新)

A.Find Divisible

You are given a range of positive integers from l to r .

Find such a pair of integers (x,y) that l≤x,y≤r , x≠y and x divides y .

If there are multiple answers, print any of them.

You are also asked to answer T independent queries.

Input

The first line contains a single integer T (1≤T≤1000 ) — the number of queries.

Each of the next T lines contains two integers l and r (1≤l≤r≤998244353 ) — inclusive borders of the range.

It is guaranteed that testset only includes queries, which have at least one suitable pair.

Output

Print T lines, each line should contain the answer — two integers x and y such that l≤x,y≤r , x≠y and x divides y . The answer in the i -th line should correspond to the i -th query from the input.

If there are multiple answers, print any of them.

程式碼:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#define inf 0x3fffffff
using namespace std;
int main()
{
    int t,x,y,m;
    cin>>t;
    while(t--)
    {
        cin>>x>>y;
        m=y/x;
        if(m>1)
            cout<<x<<" "<<2*x<<endl;
        else cout<<endl;
    }
    return 0;
}

B. Substring Removal

You are given a string ss of length nn consisting only of lowercase Latin letters.

A substring of a string is a contiguous subsequence of that string. So, string "forces" is substring of string "codeforces", but string "coder" is not.

Your task is to calculate the number of ways to remove exactly one substring from this string in such a way that all remaining characters are equal (the number of distinct characters either zero or one).

It is guaranteed that there is at least two different characters in s.

Note that you can remove the whole string and it is correct. Also note that you should remove at least one character.

Since the answer can be rather large (not very large though) print it modulo 998244353.

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 n (2≤n≤2⋅105) — the length of the string s.

The second line of the input contains the string s of length n consisting only of lowercase Latin letters.

It is guaranteed that there is at least two different characters in ss.

Output

Print one integer — the number of ways modulo 998244353 to remove exactly one substring from ss in such way that all remaining characters are equal.

分析:字串去掉子串(連續的)後各字元需要滿足的要求

1.剩餘字元數量可以為0。

2.剩餘字元中不同種字元相互間數量相等。

3.剩餘字元中存在不同的字元種類為0或1種。(比賽的時候把這條看漏的我哭唧唧)

因為去掉的子串是連續的,所以直接判斷首尾的字元種類和數量就好了。

程式碼:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#define mod 998244353
using namespace std;
int main()
{
    int n;
    long long a=1,b=1;
    string s;
    cin>>n>>s;
    while(a)
    {
        if(s[a]!=s[0]) break;
        a++;
    }
    while(b)
    {
        if(s[n-b-1]!=s[n-1]) break;
        b++;
    }
//    cout<<a<<" "<<b<<endl;
//可以檢驗一哈字元數量是不是對的
    if(s[0]==s[n-1]) cout<<(a*b+a+b+1)%mod<<endl;
    else if(s[0]!=s[n-1]) cout<<(a+b+1)%mod<<endl;
    return 0;
}

 

 

C.Polygon for the Angle

You are given an angle angang.

The Jury asks You to find such regular n-gon (regular polygon with nn vertices) that it has three vertices a, b and c (they can be non-consecutive) with ∠abc=ang or report that there is no such n-gon.

If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed 998244353.

Input

The first line contains single integer T (1≤T≤180) — the number of queries.

Each of the next T lines contains one integer ang (1≤ang<180) — the angle measured in degrees.

Output

For each query print single integer nn (3≤n≤998244353) — minimal possible number of vertices in the regular n-gon or −1 if there is no such n.

分析:假設取n邊形一點為頂點,另取兩個相鄰頂點組成n邊形的單位角p=180/n。

當ang能被p整除的話,n滿足題意。設正整數m使ang=m*p  ->  180/ang=n/m。

所以將180/ang約為最簡分數,分子為滿足題意的最小n,分母為ang角夾的邊長數。

判斷m<=n-2 輸出分子,反之輸出分子的兩倍。

程式碼:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#define inf 0x3fffffff
using namespace std;
int main()
{
    int dg(int a,int b);
    int t,x,n,m;
    cin>>t;
    while(t--)
    {
        cin>>x;
        if(x>=180) cout<<-1<<endl;
        else
        {
            m=dg(180,x);
            n=180/m;
            m=x/m;
            if(m<n-1) cout<<n<<endl;
            else cout<<2*n<<endl;
        }
    }
    return 0;
}
int dg(int a,int b)
{
    int t;
    do
    {
        t=max(a,b);
        b=min(a,b);
        a=t;
        a=a%b;
    }
    while(a);
    return b;
}

D. Easy Problem

Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length n consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements.

Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is 0, and removing i-th character increases the ambiguity by ai (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still 4 even though you delete it from the string had).

Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!

Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

Input

The first line contains one integer n (1≤n≤105) — the length of the statement.

The second line contains one string s of length n, consisting of lowercase Latin letters — the statement written by Vasya.

The third line contains n integers a1,a2,…,an(1≤ai≤998244353).

Output

Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.

分析:

if   h-0  a-1  r-2  d-3   字串為s

e(j)   :   表示字串中當前位置是 j 且是最後一個 j 的話一定滿足題意的最小偏差。

​if(s[i]=='h')  e[0]+=a[i];
//因為h是hard的首位,所以前面的h都要去掉
if(s[i]=='a')  e[1]=min(e[0],e[1]+a[i]);
//判斷是去掉前面所有h還是i-1時候的e[1]加上去掉當前位置的a偏差更小
if(s[i]=='r')  e[2]=min(e[1],e[2]+a[i]);
//同理,因為i-1時e[1]和e[0]比較過留下的e[1],所以直接和e[1]比較
if(s[i]=='d')  e[3]=min(e[2],e[3]+a[i]);
//同上

//因為d是hard的末尾,直接輸出e[3]
cout<<e[3]<<endl;
​

程式碼:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int main()
{
    int n,a[maxn],i;
    long long b=0,c=0,d=0,e=0;
    string s;
    cin>>n>>s;
    for(i=0;i<n;i++)
        cin>>a[i];
    i=-1;
    while(s[++i])
    {
        if(s[i]=='h') b+=a[i];
        else if(s[i]=='a') c=min(b,c+a[i]);
        else if(s[i]=='r') d=min(c,d+a[i]);
        else if(s[i]=='d') e=min(d,e+a[i]);
        else;
    }
    cout<<e<<endl;
    return 0;
}