結果

問題 No.2561 みんな大好きmod 998
ユーザー Keitaro Naruse
提出日時 2023-12-02 16:20:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 65 ms / 4,000 ms
コード長 1,544 bytes
コンパイル時間 646 ms
コンパイル使用メモリ 71,948 KB
最終ジャッジ日時 2025-02-18 05:21:36
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 * @file 2561.cpp
 * @brief yukicoder No.2561 みんな大好きmod 998
 * @author Keitaro Naruse
 * @date 2023-12-02
 * @copyright MIT License
 * @details https://yukicoder.me/problems/no/2561
 * */

// # Solution

#include <iostream>
#include <vector>

template < class T >
std::ostream& operator<<( std::ostream& os, const std::vector< T >& v ) {
    for( auto it = v.begin( ); it != v.end( ); it++ ) {
        os << *it << ( it == --v.end( ) ? "" : " " );
    }
    return os;
}

int dfs( int k, int from, std::vector< int >& indicies, int N, int K, const std::vector< int >& A ) {
    if( k == K ) {
        // std::cerr << indicies << std::endl;
        long long sum = 0LL;
        for( int i = 0; i < K; i++ ) {
            sum += A.at( indicies.at( i ) );
        }
        if( sum % 998244353LL <= sum % 998LL ) {
            return 1;
        }
        return 0;
    }
    int count = 0;
    for( int i = from; i < N; i++ ) {
        indicies.at( k ) = i;
        count += dfs( k + 1, i + 1, indicies, N, K, A );
    }
    return count;
}

int main( ) {
    //  Read N = [ 1, 28 ], K = min( 8, N )
    int N, K;
    std::cin >> N >> K;
    //  Read Ai = [ 0, 10^9 ]
    std::vector< int > A( N, 0 );
    for( int i = 0; i < N; i++ ) {
        std::cin >> A.at( i );
    }

    //  Main::Preprocess
    //  Main::Find a solution
    std::vector< int > indicies( K, 0 );
    int answer = dfs( 0, 0, indicies, N, K, A ) % 998;

    //  Main::Output a solution
    std::cout << answer << std::endl;

    //  Finalize
    return 0;
}
0