結果

問題 No.528 10^9と10^9+7と回文
ユーザー mugen_1337mugen_1337
提出日時 2021-04-02 16:48:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 1,000 ms
コード長 3,849 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 208,548 KB
実行使用メモリ 13,276 KB
最終ジャッジ日時 2023-08-25 01:12:03
合計ジャッジ時間 6,833 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 251 ms
12,968 KB
testcase_11 AC 248 ms
12,956 KB
testcase_12 AC 252 ms
13,028 KB
testcase_13 AC 250 ms
13,148 KB
testcase_14 AC 146 ms
9,068 KB
testcase_15 AC 128 ms
8,312 KB
testcase_16 AC 117 ms
8,040 KB
testcase_17 AC 97 ms
7,076 KB
testcase_18 AC 236 ms
12,444 KB
testcase_19 AC 66 ms
5,912 KB
testcase_20 AC 173 ms
10,128 KB
testcase_21 AC 96 ms
6,984 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 25 ms
4,376 KB
testcase_26 AC 250 ms
13,276 KB
testcase_27 AC 238 ms
13,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// a^n (mod m)
ll pow_mod(ll a,ll n,ll m){
    ll ret=1;
    while(n){
        if(n&1) ret=ret*a%m;
        a=(a*a)%m;
        n/=2;
    }
    return ret;
}


ll inv_mod(ll a,ll m){
    ll b=m,u=1,v=0,t;
    while(b){
        t=a/b;
        swap(a-=t*b,b);swap(u-=t*v,v);
    }
    u%=m;
    if(u<0) u+=m;
    return u;
}


ll solve(string s,ll mod){
    int n=(int)s.size();
    int m=n/2;
    // 0:up, 1:eq, 2:lw
    auto left=[&](int flag,int put,int dig){
        if(flag==0){
            return flag;
        }
        if(flag==1){
            if(put==dig) return 1;
            else if(put<dig) return 2;
            return 0;
        }
        if(flag==2){
            return 2;
        }
        return -1;
    };
    auto right=[&](int flag,int put,int dig){
        if(dig==put){
            return flag;
        }
        if(put<dig) return 2;
        return 0;
    };

    vector<vector<vector<ll>>> dp(m+1,vector<vector<ll>>(3,vector<ll>(3,0)));
    dp[0][1][1]=1;

    for(int i=0;i<m;i++){
        int u=s[i]-'0',d=s[n-1-i]-'0';
        for(int p=0;p<10;p++){
            if(i==0 and p==0) continue;

            rep(j,3){
                rep(k,3){
                    int lt=left(j,p,u);
                    int rt=right(k,p,d);

                    dp[i+1][lt][rt]=(dp[i+1][lt][rt]+dp[i][j][k])%mod;
                }
            }
        }
    }

    // rep(j,3)rep(k,3){
    //     cout<<j<<", "<<k<<" : "<<dp[m][j][k]<<endl;
    // }

    ll ret=0;
    if(n&1){
        int mid=s[n/2]-'0';
        rep(i,10){
            if(i<mid){
                // ret+=   dp[m][0][0]+dp[m][0][0]+dp[m][0][0]+
                //         dp[m][1][0]+dp[m][1][1]+dp[m][1][2]+
                //         dp[m][2][0]+dp[m][2][1]+dp[m][2][2];

                ret+=   
                        dp[m][1][0]+dp[m][1][1]+dp[m][1][2]+
                        dp[m][2][0]+dp[m][2][1]+dp[m][2][2];
                     
                ret%=mod;
            }else if(i==mid){
                ret+=   
                                    dp[m][1][1]+dp[m][1][2]+
                        dp[m][2][0]+dp[m][2][1]+dp[m][2][2];
                ret%=mod;
            }else{
                ret+=   
                        
                        dp[m][2][0]+dp[m][2][1]+dp[m][2][2];
                ret%=mod;
            }
        }
    }else{
        ret+=   
                dp[m][1][1]+dp[m][1][2]+
                dp[m][2][0]+dp[m][2][1]+dp[m][2][2];
        ret%=mod;
    }
    return ret;
}

void unk(string s,ll mod){
    int n=(int)s.size();

    if(n==1){
        cout<<(s[0]-'0')<<endl;
        return ;
    }

    ll res=0;
    for(int i=1;i<n;i++){
        int fr=i/2+i%2-1;
        ll add=pow_mod(10,fr,mod)*9%mod;
        res=(res+add)%mod;
    }
    res+=solve(s,mod);
    res%=mod;
    cout<<res<<endl;
}

signed main(){
    string s;cin>>s;
    unk(s,1000000000);
    unk(s,1000000007);
    return 0;
}
0