제출 #369085

#제출 시각아이디문제언어결과실행 시간메모리
369085cpp219Palindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
2 ms640 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 9;
const ll inf = 1e16 + 7;
typedef pair<int,int> LL;
string s;
ll n,k,dp[20][12][12][2][2],L,R;

string To_string(ll x){
    string res;
    while(x > 0) res += char(x%10 + '0'),x /= 10;
    reverse(res.begin(),res.end());
    return res;
}

ll f(ll num,ll prv1,ll prv2,ll smaller,ll st){
    if (num > n) return 1;
    if (dp[num][prv1][prv2][smaller][st] != -1) return dp[num][prv1][prv2][smaller][st];
    ll ans = 0,now = s[num] - '0';
    for (ll i = 0;i <= 9;i++){
        if (!smaller && i > now) break;
        if (!st){
            if (!i) ans += f(num + 1,prv1,prv2,1,0);
            else{
                if (i < now || smaller) ans += f(num + 1,i,prv1,1,1);
                else if (i == now) ans += f(num + 1,i,prv1,0,1);
            }
        }
        else{
            if (i != prv1 && i != prv2){
                if (i < now || smaller) ans += f(num + 1,i,prv1,1,1);
                else if (i == now) ans += f(num + 1,i,prv1,0,1);
            }
        }
    }
    return dp[num][prv1][prv2][smaller][st] = ans;
}

ll cal(ll x){
    memset(dp,-1,sizeof(dp));
    if (x < 0) return 0;
    if (!x) return 1;
    s = To_string(x); s = " " + s; n = s.size() - 1;
    //cout<<f(1,10,10,0,0); exit(0);
    return f(1,10,10,0,0);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #define task "tst"
    if (fopen(task".INP","r")){
        freopen(task".INP","r",stdin);
        //freopen(task".OUT","w",stdout);
    }
    cin>>L>>R;
    //cout<<cal(9); return 0;
    cout<<cal(R) - cal(L - 1);
}

컴파일 시 표준 에러 (stderr) 메시지

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