結果

問題 No.493 とても長い数列と文字列(Long Long Sequence and a String)
ユーザー momoyuumomoyuu
提出日時 2023-03-19 20:29:21
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 800 ms
コード長 3,340 bytes
コンパイル時間 3,197 ms
コンパイル使用メモリ 254,792 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-18 13:52:17
合計ジャッジ時間 6,186 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 115
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//const ll mod = 998'244'353;
const ll mod = 1'000'000'007;
//const ll mod = 67'280'421'310'721;
struct mint{
    long long x;
    mint(long long x=0):x((x%mod+mod)%mod){}
    mint operator-() const{
        return mint(-x);
    }
    mint& operator+=(const mint& a){
        if((x+=a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator-=(const mint& a){
        if((x+=mod-a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator*=(const  mint& a){
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const{
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const{
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const{
        mint res(*this);
        return res*=a;
    }
    mint pow(long long n) const {
        assert(0 <= n);
        mint a = *this, r = 1;
        while (n) {
            if (n & 1) r *= a;
            a *= a;
            n >>= 1;
        }
        return r;
    }
    mint inv() const{
        return pow(mod-2);
    }
    mint& operator/=(const mint& a){
        return (*this)*=a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    bool operator==(const mint& a) const {
        return x == a.x;
    }
    bool operator<(const mint& a) const{
        return x < a.x;
    }
};
ll k,l,r;
vector<ll> len,sum;
vector<mint> mul;
void calc(ll now){
    ll w = now * now;
    ll s = 0;
    ll le = 0;
    mint m = 1;
    while(w){
        ll a = w % 10;
        w /= 10;
        if(a==0) a = 10;
        le++;
        s += a;
        m *= mint(a);
    }
    le += 2 * len[now-1];
    s += 2LL * sum[now-1];
    m *= mul[now-1] * mul[now-1];
    len.push_back(le);
    sum.push_back(s);
    mul.push_back(m);
    if(le<r) calc(now+1);
    else return;
}

pair<ll,mint> c(ll m,ll a){
    if(a==0) return make_pair(0LL,mint(1));
    if(m==1){
        assert(a==1);
        return make_pair(sum[m],mul[m]);
    }
    if(len[m-1]>=a) return c(m-1,a);
    ll l = len[m-1];
    ll b = len[m] - 2*len[m-1];
    if(l+b<=a){
        pair<ll,mint> now = make_pair(sum[m]-sum[m-1],mul[m]*(mul[m-1].inv()));
        pair<ll,mint> nxt = c(m-1,a-l-b);
        return make_pair(now.first+nxt.first,now.second*nxt.second);
    }else{
        pair<ll,mint> now = make_pair(sum[m-1],mul[m-1]);
        vector<ll> use;
        ll w = m * m;
        while(w){
            ll mm = w % 10;
            w /= 10;
            if(mm==0) mm = 10;
            use.push_back(mm);
        }
        reverse(use.begin(),use.end());
        for(int i = 0;i<a-l;i++){
            now.first += use[i];
            now.second *= mint(use[i]);
        }
        return now;
    }
}

pair<ll,mint> solve(ll a){
    ll mx = len.size()-1;
    return c(mx,a);
}

int main(){
    cin>>k>>l>>r;
    len.push_back(0);
    sum.push_back(0);
    mul.push_back(1);
    calc(1);
    ll mx = len.size()-1;
    if(mx>k){
        cout<<-1<<endl;
        return 0;
    }
    pair<ll,mint> L = solve(l-1);
    pair<ll,mint> R = solve(r);
    cout<<R.first-L.first<<" "<<R.second*(L.second.inv())<<endl;
}

0