Codeforces Round #411 (Div. 2)(A+B+C+D)

来源:互联网 发布:公路工程计量软件 编辑:程序博客网 时间:2024/06/04 18:59

A. Fake NP
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.

You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.

Solve the problem to show that it’s not a NP problem.

Input
The first line contains two integers l and r (2 ≤ l ≤ r ≤ 109).

Output
Print single integer, the integer that appears maximum number of times in the divisors.

If there are multiple answers, print any of them.

Examples
input
19 29
output
2
input
3 6
output
3
Note
Definition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.html

The first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.

The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}.

题意:给出l和r求l到r之间每个数因子最多的是几?
题解:相同就是l否则就是2.
代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <string>#include <vector>using namespace std;#define inf 0x3f3f3f3f#define LL long longint main(){    int a,b;    scanf("%d%d",&a,&b);    if(a==b) printf("%d\n",a);    else printf("2\n");    return 0;}

B. 3-palindrome
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In the beginning of the new year Keivan decided to reverse his name. He doesn’t like palindromes, so he changed Naviek to Navick.

He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either ‘a’, ‘b’ or ‘c’, with no palindromes of length 3 appearing in the string as a substring. For example, the strings “abc” and “abca” suit him, while the string “aba” doesn’t. He also want the number of letters ‘c’ in his string to be as little as possible.

Input
The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.

Output
Print the string that satisfies all the constraints.

If there are multiple answers, print any of them.

Examples
input
2
output
aa
input
3
output
bba
Note
A palindrome is a sequence of characters which reads the same backward and forward.

题意:构造出一个由a,b,c组成长度为n且没有长度>=3的回文子串的串,c尽可能少。
题解:明显构造成aabbaabb这种就行。
代码:

#include<bits/stdc++.h>#define ll long longusing namespace std;int main(){    int n;    cin>>n;    for(int i=1;i<=n;i++)    {        if(i%4==1||i%4==2) cout<<"a";        else cout<<"b";    }    cout<<endl;}

C. Find Amir
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.

There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.

Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of schools.

Output
Print single integer: the minimum cost of tickets needed to visit all schools.

Examples
input
2
output
0
input
10
output
4
Note
In the first example we can buy a ticket between the schools that costs .

题意:n个学校,两点之间的消耗为(i+j)%(n+1)。起点终点任意,问遍历所有学校的最小消耗。
题解:消耗小,就尽可能让i+j==n+1.则1先到n(无消耗),n到(2)(消耗为1),2到n-1(消耗为1)。。。
代码:

#include<bits/stdc++.h>#define ll long longusing namespace std;int main(){    int n;    cin>>n;    cout<<(n-1)/2<<endl;}

D. Minimum number of steps
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
We have a string of letters ‘a’ and ‘b’. We want to perform some operations on it. On each step we choose one of substrings “ab” in the string and replace it with the string “bba”. If we have no “ab” as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.

The string “ab” appears as a substring if there is a letter ‘b’ right after the letter ‘a’ somewhere in the string.

Input
The first line contains the initial string consisting of letters ‘a’ and ‘b’ only with length from 1 to 106.

Output
Print the minimum number of steps modulo 109 + 7.

Examples
input
ab
output
1
input
aab
output
3
Note
The first example: “ab”  →  “bba”.

The second example: “aab”  →  “abba”  →  “bbaba”  →  “bbbbaa”.
题意:出现ab就用bba代替,问操作次数。
题解:

串 消耗 新串 ab 1 bba aab 3 bbbbaa aaab 7 bbbbbbbbaaa

明显消耗为2^i-1.(i为a的个数)对于每一个b判断前面a的个数。然后求和。
代码:

#include<bits/stdc++.h>#define ll long longusing namespace std;const ll MOD=1e9+7;char a[10010000];ll solo(ll x,int y){    ll sum=1;    while(y)    {        if(y&1)        sum=(sum*x)%MOD;        x=(x*x)%MOD;        y>>=1;    }    return sum-1;}int main(){    scanf("%s",a);    int len=strlen(a);    ll ans=0;    int ge=0;    for(int i=0;i<len;i++)    {        if(a[i]=='a')        {            ge++;        }        else        {            if(ge==0) continue;            ans+=solo(2,ge);            ans=ans%MOD;        }    }    printf("%I64d\n",ans%MOD);}
0 0
原创粉丝点击