Submission #1096390

# Submission time Handle Problem Language Result Execution time Memory
1096390 2024-10-04T11:32:02 Z slah007 Weird Numeral System (CCO21_day1problem2) C++17
25 / 25
2098 ms 1640 KB
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
using namespace std;

const int MAXK = 1e6;
const int MAXM = 2500;
const int MAXD = MAXM<<1|1;
const int NAN = 998244353;

int K, Q, D, M;
set<int> digits;

map<long long, int> dp;
int go(long long n){
    if(dp.count(n)) return dp[n];
    if(digits.count(n)){
        return dp[n] = n;
    }
    dp[n] = NAN;
    for(auto x : digits){
        if((n-x)%K == 0){
            long long t = (n-x)/K;
            if(go(t) != NAN){
                return dp[n] = x;
            }
        }
    }
    return NAN;
}

int main(){
    scanf("%d %d %d %d", &K, &Q, &D, &M);
    for(int i=0;i<D;i++){
        int x;
        scanf("%d", &x);
        digits.insert(x);
    }
    while(Q--){
        long long n;
        scanf("%lld", &n);
        dp.clear();
        if(go(n) != NAN){
            vector<int> v;
            do{
                v.push_back(dp[n]);
                n = (n - dp[n]) / K;
            }while(n);
            reverse(v.begin(), v.end());
            for(int i=0;i<(int)v.size();i++){
                printf("%d%c", v[i], " \n"[i+1==(int)v.size()]);
            }
        }
        else{
            puts("IMPOSSIBLE");
        }
    }
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     scanf("%d %d %d %d", &K, &Q, &D, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         scanf("%d", &x);
      |         ~~~~~^~~~~~~~~~
Main.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |         scanf("%lld", &n);
      |         ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB OK
2 Correct 1 ms 348 KB OK
3 Correct 1 ms 348 KB OK
4 Correct 0 ms 348 KB OK
5 Correct 0 ms 344 KB OK
6 Correct 1 ms 348 KB OK
7 Correct 1 ms 348 KB OK
8 Correct 0 ms 600 KB OK
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB OK
2 Correct 1 ms 348 KB OK
3 Correct 1 ms 348 KB OK
4 Correct 0 ms 348 KB OK
5 Correct 0 ms 344 KB OK
6 Correct 1 ms 348 KB OK
7 Correct 1 ms 348 KB OK
8 Correct 0 ms 600 KB OK
9 Correct 1 ms 348 KB OK
10 Correct 1 ms 348 KB OK
11 Correct 0 ms 596 KB OK
12 Correct 0 ms 348 KB OK
13 Correct 0 ms 348 KB OK
14 Correct 1 ms 348 KB OK
15 Correct 0 ms 348 KB OK
16 Correct 0 ms 348 KB OK
17 Correct 0 ms 348 KB OK
18 Correct 0 ms 432 KB OK
19 Correct 1 ms 348 KB OK
20 Correct 0 ms 348 KB OK
21 Correct 68 ms 1620 KB OK
22 Correct 427 ms 1616 KB OK
23 Correct 2098 ms 1640 KB OK
24 Correct 847 ms 1564 KB OK
25 Correct 1 ms 344 KB OK
26 Correct 0 ms 348 KB OK
27 Correct 0 ms 348 KB OK
28 Correct 0 ms 348 KB OK