Submission #631311

# Submission time Handle Problem Language Result Execution time Memory
631311 2022-08-18T03:07:42 Z Tuanlinh123 Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 340 KB
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

#define LOCALIO "C:/Users/admin/Documents/Code/freopen/"

ll dp[20][10][10];

ll calc(ll a)
{
    if (a==-1)
        return 0;
    if (a<10)
        return a+1;
    vector <ll> num;
    while (a>0)
    {
        num.pb(a%10);
        a/=10;
    }
    reverse(num.begin(), num.end());
    ll last=-1, last2=-1, last3=-1, ans=0, n=num.size();
    for (ll i=0; i<num.size(); i++)
    {
        if ((last3==last || last2==last) && last!=-1)
            break;
        ll dig=num[i];
        if (i==0)
        {
            for (ll j=1; j<dig; j++)
                for (ll k=0; k<10; k++)
                {
                    // cout << n-i << " " << j << " " << k << " " << dp[n-i][j][k] << "\n"; 
                    ans+=dp[n-i][j][k];
                }
            last3=last2; last2=last; last=dig;
            continue;
        }
        if (i==1)
        {
            for (ll j=0; j<dig+(i==n-1); j++)
            {
                // cout << n-i+1 << " " << last << " " << j << " " << dp[n-i+1][last][j] << "\n";
                ans+=dp[n-i+1][last][j];
            }
            last3=last2; last2=last; last=dig;
            continue;
        }
        if (i==n-1)
        {
            for (ll j=0; j<=dig; j++)
                if (j!=last2)
                {
                    // cout << n-i+1 << " " << last << " " << j << " " << dp[n-i+1][last][j] << "\n";
                    ans+=dp[n-i+1][last][j];
                }
            last3=last2; last2=last; last=dig;
            continue;
        }
        for (ll j=0; j<dig; j++)
            if (j!=last2)
            {
                // cout << n-i+1 << " " << last << " " << j << " " << dp[n-i+1][last][j] << "\n";
                ans+=dp[n-i+1][last][j];
            }
        last3=last2; last2=last; last=dig;
    }
    // cout << ans << " ";
    for (ll i=1; i<n; i++)
    {
        if (i==1)
        {
            ans+=10;
            continue;
        }
        for (ll j=1; j<10; j++)
            for (ll k=0; k<10; k++)
            {
                // cout << i << " " << j << " " << k << " " << dp[i][j][k] << "\n";
                ans+=dp[i][j][k];
            }
    }
    // for (ll i=0; i<num.size(); i++)
    //     cout << num[i];
    // cout << " ";
    // cout << ans << "\n";
    return ans;
}

int main()
{
    #ifdef LOCAL
        freopen( LOCALIO "input.txt","r",stdin) ;
        freopen( LOCALIO "output.txt","w",stdout) ;
    #endif

    ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
//	freopen("FIBONACCI.inp","r",stdin);
//	freopen("FIBONACCI.out","w",stdout);
    for (ll i=2; i<20; i++)
    {
        for (ll j=0; j<10; j++)
        {
            for (ll k=0; k<10; k++)
            {
                if (j==k)
                {
                    dp[i][j][k]=0;
                    continue;
                }
                if (i==2)
                {
                    dp[i][j][k]=1;
                    continue;
                }
                for (ll z=0; z<10; z++)
                    if (z!=j)
                        dp[i][j][k]+=dp[i-1][k][z];
                // cout << i << " " << j << " " << k << " " << dp[i][j][k] << "\n";
            }
        }
    }
    ll a, b; cin >> a >> b;
    cout << calc(b)-calc(a-1);
}

Compilation message

numbers.cpp: In function 'long long int calc(long long int)':
numbers.cpp:30:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for (ll i=0; i<num.size(); i++)
      |                  ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 272 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct