Submission #1275150

#TimeUsernameProblemLanguageResultExecution timeMemory
1275150chanhchuong123Trol (COCI19_trol)C++20
50 / 50
4 ms576 KiB
#include<bits/stdc++.h>
using namespace std;

const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)

template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) a = b; else return 0; return 1;
}

long long dp[20][2][2][200];
int fin[200];
int n;
int a[20];
int b[20];

long long DP(int pos, bool lower, bool upper, int sum) {
    if (pos < 0) return fin[sum]; long long &res = dp[pos][lower][upper][sum];
    if (res != -1) return res;
    res = 0;
    int lo = !lower ? a[pos] : 0;
    int hi = !upper ? b[pos] : 9;
    for (int c = lo; c <= hi; ++c) {
         res += DP(pos - 1, lower | a[pos] < c, upper | c < b[pos], sum + c);
    }
    return res;
}

void process(void) {
    for (int i = 1; i < 200; ++i) {
        int x = i;
        while (x > 9) {
            int y = 0; while (x > 0) {
                y += x % 10;
                x /= 10;
            }
            x = y;
        }
        fin[i] = x;
    }
    int q; cin >> q;
    while (q--) {
        memset(dp, -1, sizeof dp);
        long long l, r;
        cin >> l >> r;
        n = 0;
        while (r > 0) {
            a[n] = l % 10; l /= 10;
            b[n] = r % 10; r /= 10;
            n++;
        }
        cout << DP(n - 1, 0, 0, 0) << '\n';
    }
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r",  stdin);
		freopen(task".out", "w", stdout);
	}

	int nTest = 1; if (multiTest) cin >> nTest;
	while (nTest--) {
		process();
	}

	return 0;
}

Compilation message (stderr)

trol.cpp: In function 'int main()':
trol.cpp:68:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |                 freopen(task".inp", "r",  stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
trol.cpp:69:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |                 freopen(task".out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...