Submission #1082823

#TimeUsernameProblemLanguageResultExecution timeMemory
1082823lamlamlamPalindrome-Free Numbers (BOI13_numbers)C++17
72.50 / 100
1 ms604 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int a,b,dp[20][2][11][11],n;
string s;
int f(int pos,int tight,int prev1,int prev2)
{
    if(pos==n) return 1;
    if(dp[pos][tight][prev1][prev2]!=-1) return dp[pos][tight][prev1][prev2];
    int res = 0, ub = 9;
    if(tight) ub = s[pos] - '0';
    for(int i=0; i<=ub; i++){
        if(i==prev1 or i==prev2) continue;
        int is_tight = (tight && i==ub);
        res += f(pos+1,is_tight,i,prev1);
    }
    return dp[pos][tight][prev1][prev2] = res;
}

int sol(int x)
{
    for(int i=0; i<20; i++) for(int j=0; j<11; j++) for(int k=0; k<11; k++) dp[i][0][j][k] = dp[i][1][j][k] = -1;
    s = to_string(x);
    n = s.size();
    return f(0,1,10,10);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #define task "troll"
    if(fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        freopen(task".out","w",stdout);
    }
    cin >> a >> b;
    cout << sol(b) - sol(a-1);
    cerr << "\nTime: " << clock();
}

Compilation message (stderr)

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