AtCoder Grand Contest 022

A - Diverse Word


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoderzscoderand agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X=x1x2…xn and Y=y1y2…ym be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or xj>yj where j is the smallest integer such that xjyj.

Constraints

  • 1≤|S|≤26
  • S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.


Sample Input 1

Copy
atcoder

Sample Output 1

Copy
atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.


Sample Input 2

Copy
abc

Sample Output 2

Copy
abcd

Sample Input 3

Copy
zyxwvutsrqponmlkjihgfedcba

Sample Output 3

Copy
-1

This is the lexicographically largest diverse word, so the answer is -1.


Sample Input 4

Copy
abcdefghijklmnopqrstuvwzyx

Sample Output 4

Copy
abcdefghijklmnopqrstuvx

这个比赛也挺棒的啊,但是A题我就惨了啊

一个字符串存不存在下一个全排列,存在的话输出-1,否则输出他最小的,这个方法很棒,要不然得分三种情况

#include <bits/stdc++.h>
using namespace std;
int a[256];
char s[256];
int main()
{
    cin>>s;
    int l=1;
    for(int i=0; s[i]; i++)a[s[i]]=1,l++;
    for(int c='z'; c>='a'; c--)
        if(!a[c])s[l++]=c;
    if(next_permutation(s,s+l))
        cout<<s;
    else
        cout<<-1;
    return 0;
}

 

B - GCD Sequence


Time limit : 2sec / Memory limit : 256MB

Score : 600 points

Problem Statement

Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers.

She thinks that a set S={a1,a2,…,aN} of distinct positive integers is called special if for all 1≤iN, the gcd (greatest common divisor) of ai and the sum of the remaining elements of S is not 1.

Nagase wants to find a special set of size N. However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a special set of size N such that the gcd of all elements are 1 and the elements of the set does not exceed 30000.

Constraints

  • 3≤N≤20000

Input

Input is given from Standard Input in the following format:

N

Output

Output N space-separated integers, denoting the elements of the set SS must satisfy the following conditions :

  • The elements must be distinct positive integers not exceeding 30000.
  • The gcd of all elements of S is 1, i.e. there does not exist an integer d>1 that divides all elements of S.
  • S is a special set.

If there are multiple solutions, you may output any of them. The elements of S may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.


Sample Input 1

Copy
3

Sample Output 1

Copy
2 5 63

{2,5,63} is special because gcd(2,5+63)=2,gcd(5,2+63)=5,gcd(63,2+5)=7. Also, gcd(2,5,63)=1. Thus, this set satisfies all the criteria.

Note that {2,4,6} is not a valid solution because gcd(2,4,6)=2>1.


Sample Input 2

Copy
4

Sample Output 2

Copy
2 5 20 63

{2,5,20,63} is special because gcd(2,5+20+63)=2,gcd(5,2+20+63)=5,gcd(20,2+5+63)=10,gcd(63,2+5+20)=9. Also, gcd(2,5,20,63)=1. Thus, this set satisfies all the criteria.


让你构造一个序列,首先这个序列的值的gcd值为1,其他的每一个数和其他数的和的gcd不是1

 这个构造好难啊,看到别人的那个序列才知道还有这种操作

#include<bits/stdc++.h>
using namespace std;
int n,t[9];
int main()
{
    scanf("%d",&n);
    if(n==3)printf("2 5 63");
    else
    {
        if(n&1)
            t[0]=6,t[1]=2,t[2]=10,t[3]=3,t[4]=9,t[5]=4,t[6]=8,t[7]=12;
        else
            t[0]=2,t[1]=10,t[2]=3,t[3]=9,t[4]=4,t[5]=8,t[6]=6,t[7]=12;
        for(int i=0; i<min(n,8); i++)printf("%d ",t[i]);
        for(int i=8; i<n; i++)t[i%8]=t[i%8]+12,printf("%d ",t[i%8]);
    }
    return 0;
}

 

posted @ 2018-04-01 22:53  暴力都不会的蒟蒻  阅读(482)  评论(0编辑  收藏  举报