Codeforces Round #411 (Div. 2) 题解 总结(未完待续)

智商题专项训练2333333

A - Fake NP

  CodeForces - 805A

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

给你两个数,求这个区间中的每个数的因子(除了1)里出现次数最多的,如果有重复,打印任意一个。。

这。。 如果这个区间两端点不相等。第一种情况:区间一端为奇数,一端为偶数,那2的出现次数是t/2,次数最多。

第二种情况:区间一端为偶数,另一端还是偶数,不用想。

第二种情况,区间长度t为奇数,那2的出现次数是(t-1)/2次,而相邻的奇数的公因数没有能比2出现的还多的了。。因为奇数的约束条件不如偶数这么直接。

相等的话直接输出这个数就行。。

#include <iostream>
using namespace std;
int main()
{
    long long m,n;
    cin>>m>>n;
    if(m==n)
    {
        cout<<m<<endl;
    }
    else
    {
        cout<<2<<endl;
    }
    
}


B - 3-palindrome

  CodeForces - 805B

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

这题被卡了好久,果然智商余额不足了。。

只有abc三个字母的字符串,要求不出现长度为3的回文串,且c的出现次数要尽可能少。

刚开始模拟做,全填充a,顺着模拟,和前两个的那位 后两个的那位比,不行就换a,换b,换c。

最后一看aabbaabb。。这不就完美符合题意吗。。

aabbaabb作为字串无限循环。。

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    string res="aabb";
    int left=n%4;
    int time=n/4;
    while(time--)
    {
        cout<<res;
    }
    for(int i=0;i<left;i++)
    {
        cout<<res[i];
    }
    cout<<endl;

    return 0;
}


C - Find Amir

  CodeForces - 805C 

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


题意:只要全部连通就行,不要求直接到达,求车票钱最小


作图找规律,偶数连接第一个和最后一个,一直连到中间。然后顺次连接右半边。

奇数类似。

#include <iostream>
using namespace std;
int main() 
{
    int n;
    cin>>n;
    if(n%2==0)
    {
        cout<<n/2-1<<endl;
    }
    else
    {
        cout<<n/2<<endl;
    }

    return 0;
}


D - Minimum number of steps

 CodeForces - 805D

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".


这题好玩啊。。刚看见我就想到pat甲级里面打印pat那题。。有点类似,都是倒着找。

给一个只由ab两个字母构成的字符串,只要出现ab子串,就把ab变成bba,直到最后没有出现ab为止。

这题一定要倒着循环找,然后让循环过的子串成为bbbbbaaa这样的形式。

其实每一次变换,就是把a和a之后的b掉个,再在a前面补一个b。相当于后面多一个b,前面的a就要多调换一次,多一个a,前面的a的调换次数x2(a的调换需要再补一个b)

所以思路就是倒着循环找b,找a,因为a的调换次数是和后面的b的数量相关的,和ab的相对位置没有关系(遍历过的子串全部变成bbbaa这样的形式)

#include <iostream>
using namespace std;
int main()
{
    long long mod=1e9+7;
    long long temp=0;
    long long res=0;
    string a;
    cin>>a;
    int len=a.size();
    for(int i=len-1;i>=0;i--)
    {
        if(a[i]=='b')
        {
            temp++;
            temp%=mod;
        }
        else
        {
            res+=temp;
            temp*=2;
            temp%=mod;
            res%=mod;
        }
    }
    cout<<res<<endl;

    return 0;
}


坑向,尚未填坑


猜你喜欢

转载自blog.csdn.net/neuq_zsmj/article/details/80296019
今日推荐