CSES - Datatähti 2025 alku - Results
Submission details
Task:Kortit II
Sender:NoelMatero
Submission time:2024-11-07 14:51:47 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void precompute_factorials(std::vector<int>&)':
input/code.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for (int i = 1; i < factorial.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:57:9: error: redeclaration of 'int a'
   57 |     int a = permutations(p1, a, b);
      |         ^
input/code.cpp:45:12: note: 'int a' previously declared here
   45 |     int n, a, b;
      |            ^

Code

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

const int MOD = 1e9 + 7;

void precompute_factorials(vector<int>& factorial) {
    factorial[0] = 1;
    for (int i = 1; i < factorial.size(); i++) {  
        factorial[i] = (1LL * factorial[i - 1] * i) % MOD;
    }
}

int permutations(const vector<int>& p1, int a, int b) {
    int n = p1.size();
    int validPermutations = 0;
    
    vector<int> p2(n);
    for (int i = 0; i < n; ++i) {
        p2[i] = i + 1;
    }
    
    do {
        int p1Wins = 0, p2Wins = 0;
                
        for (int i = 0; i < n; ++i) {
            if (p1[i] > p2[i]) {
                p1Wins++;
            } else if (p1[i] < p2[i]) {
                p2Wins++;
            }
        }
                
        if (p1Wins == a && p2Wins == b) {
            validPermutations++;
        }

    } while (next_permutation(p2.begin(), p2.end()));  

    return validPermutations;
}

int main() {
    int n, a, b;
    cin >> n >> a >> b;

    vector<int> factorial(n + 1); 
    vector<int> p1(n);

    precompute_factorials(factorial);

    for (int i = 0; i < n; i++) {
        p1[i] = i + 1;
    }
        
    int a = permutations(p1, a, b);
    cout << (1LL * a * factorial[n]) % MOD << endl;
    
    return 0;
}