[PYTHON] AtCoder Beginner Contest 066 Review past questions

Time required

スクリーンショット 2020-01-15 13.38.16.png

Problem A

Just choose the two smaller ones.

answerA.py


a,b,c=map(int,input().split())
x=[a,b,c]
print(sum(x)-max(x))

B problem

Just erase the end in order and think about it.

answerB.py


s=input()
l=len(s)
for i in range(1,l//2):
    k=(l-i*2)
    if s[:k//2]==s[k//2:k]:
        print(k)
        break

C problem

Considering the ** actual push **, you can see that the odd-numbered and even-numbered pushes have different arrangements (see sample), so if you implement the arrangement obediently, it will be as follows. .. (It is also necessary to note that there are cases due to the evenness of n.)

answerC.py


n=int(input())
a=[i for i in input().split()]

b=[]
c=[]
if n%2==0:
    for i in range(n):
        if i%2==0:
            b.append(a[i])
        else:
            c.append(a[i])
else:
    for i in range(n):
        if i%2==0:
            c.append(a[i])
        else:
            b.append(a[i])
c=c[::-1]
print(" ".join(c)+" "+" ".join(b))

D problem

The policy was set normally, but the implementation could not be completed. (** Mounting power ** is one of the issues, but it seems that we have to handle the amount ...) It was discovered a few hours later that I was unaware of the basic mistakes in the basics.

Negative remainders behave unintentionally! !!

As you can see by referring to this article, it seems that there are multiple definitions for the remainder, and each language behaves differently. It seems. To avoid this, you can change the negative number to a positive number as written in the code. (I think it's basic, but I didn't know about C ++ because I didn't know much about it. I should study C ++ more properly ...)

As for the policy, if you count the subsequence of length k from the whole including duplication, it will be $ _ {n + 1} C _k $, so ** Experiment considering whether there may be duplication ** To do. According to the experiment, let x be the number that exists in two, and when x is in the index i, j (0 <= i, j <= N), when both of them are included in the subsequence, they may overlap. If it doesn't exist and neither of the two is included in the subsequence, you can see that there is no duplication as well. Now consider ** when you want to include only one x in a subsequence **. Considering when the number of parts i + 1 to j-1 is included, the number is located on the right side for x in i and the number is located on the left side for x in j, so the different part You can see that it is divided as a column. Therefore, the numbers in ** 0 ~ i-1, j + 1 ~ N and the subsequence selected from either x are duplicated **, and $ _ {i + (nj)} C _ {k -1} $ is the number of overlapping subsequences for subsequences of length k. Therefore, you can find $ _ {n + 1} C k- {i + (nj)} C _ {k-1} $, but as I wrote earlier, do not generate a negative remainder. In the case of a negative number, you can reduce it to a positive remainder by adding MODs.

In addition, the calculation of the combination is borrowed from Mr. Kencho. For details, please refer to this article.

answerD.cc


#include<iostream>
#include<vector>
#include<utility>
using namespace std;

const int MAX = 1000000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

//Pretreatment to make a table
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

//Binomial coefficient calculation
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(){
  //Preprocessing
  COMinit();
  int n;cin >> n;
  vector<int> a(n+1);for(int i=0;i<n+1;i++){cin >> a[i];a[i]-=1;}
  vector<bool> b(n,true);
  for(int i=0;i<n+1;i++){b[a[i]]=!b[a[i]];}
  pair<int,int> p=make_pair(-1,-1);
  for(int i=0;i<n+1;i++){
    if(b[a[i]]){
      if(p.first==-1){p.first=i;}
      else{p.second=i;break;}
    }
  }
  //At first, c was output in the following part, but c<Since there is a possibility of 0, there is not exactly the remainder of the MOD
  for(int i=1;i<=n+1;i++){
    int c=COM(n+1,i)-COM(p.first+(n-p.second),i-1);
    while(c<0){
      c+=MOD;
    }
    cout << c%MOD << endl;
  }
}

Recommended Posts

AtCoder Beginner Contest 066 Review past questions
AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 045 Review of past questions
AtCoder Beginner Contest 120 Review of past questions
AtCoder Beginner Contest 108 Review of past questions
AtCoder Beginner Contest 106 Review of past questions
AtCoder Beginner Contest 122 Review of past questions
AtCoder Beginner Contest 125 Review of past questions
AtCoder Beginner Contest 109 Review of past questions
AtCoder Beginner Contest 118 Review of past questions