Submission #230433

#TimeUsernameProblemLanguageResultExecution timeMemory
230433mehrdad_sohrabiPalindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
5 ms512 KiB
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; #define pb push_back #define pii pair < ll , ll > #define F first #define S second #define endl '\n' #define int long long #define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") #define kill(x) return cout<<x<<'\n', 0; using namespace std; const int N=20,M=10; bool check(vector <int> a){ for (int i=1;i<a.size();i++){ if (a[i]==a[i-1]) return 0; } for (int i=2;i<a.size();i++){ if (a[i]==a[i-2]) return 0; } return 1; } ll dp[N]; ll p[N]; ll solve(ll x){ if (x<10) return x+1; vector <int> a; ll k=x; while(k){ a.pb(k%10); k/=10; } ll ans=0; ll z=a.size(); reverse(a.begin(),a.end()); vector <int> b; for (int i=0;i<a.size();i++){ z--; for (int j=0;j<a[i];j++){ if (i==0 && j==0) continue; b.pb(j); if (check(b)){ if (i==0){ ans+=p[z]/8*9; // cout << p[z]/8*9 << endl;; b.pop_back(); continue; } ans+=p[z]; // cout << p[z] << endl; } b.pop_back(); } b.pb(a[i]); } if (check(b)) ans++; ans+=dp[(ll)a.size()-1]; return ans; } int32_t main(){ p[0]=1; for (int i=1;i<N;i++){ p[i]=p[i-1]*8; } dp[0]=1; dp[1]=10; for (int i=2;i<N;i++){ dp[i]=dp[i-1]+9*9*p[i-2]; } ll l,r; cin >> l >> r; ll ans=solve(r); // cout << ans << endl; if (l!=0) ans-=solve(l-1); cout << ans << endl; }

Compilation message (stderr)

numbers.cpp: In function 'bool check(std::vector<long long int>)':
numbers.cpp:17:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=1;i<a.size();i++){
                  ~^~~~~~~~~
numbers.cpp:20:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=2;i<a.size();i++){
                  ~^~~~~~~~~
numbers.cpp: In function 'll solve(ll)':
numbers.cpp:39:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0;i<a.size();i++){
                  ~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...