結果

問題 No.493 とても長い数列と文字列(Long Long Sequence and a String)
ユーザー imulanimulan
提出日時 2017-03-12 04:35:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 800 ms
コード長 4,758 bytes
コンパイル時間 1,702 ms
コンパイル使用メモリ 171,120 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 08:21:31
合計ジャッジ時間 5,287 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 1 ms
4,376 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 1 ms
4,380 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 1 ms
4,376 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 1 ms
4,380 KB
testcase_68 AC 1 ms
4,380 KB
testcase_69 AC 2 ms
4,376 KB
testcase_70 AC 2 ms
4,376 KB
testcase_71 AC 1 ms
4,376 KB
testcase_72 AC 1 ms
4,376 KB
testcase_73 AC 2 ms
4,380 KB
testcase_74 AC 2 ms
4,376 KB
testcase_75 AC 1 ms
4,380 KB
testcase_76 AC 1 ms
4,380 KB
testcase_77 AC 1 ms
4,380 KB
testcase_78 AC 1 ms
4,376 KB
testcase_79 AC 1 ms
4,380 KB
testcase_80 AC 1 ms
4,376 KB
testcase_81 AC 1 ms
4,376 KB
testcase_82 AC 2 ms
4,376 KB
testcase_83 AC 2 ms
4,376 KB
testcase_84 AC 1 ms
4,380 KB
testcase_85 AC 2 ms
4,376 KB
testcase_86 AC 2 ms
4,380 KB
testcase_87 AC 2 ms
4,376 KB
testcase_88 AC 1 ms
4,380 KB
testcase_89 AC 2 ms
4,376 KB
testcase_90 AC 1 ms
4,376 KB
testcase_91 AC 1 ms
4,380 KB
testcase_92 AC 2 ms
4,376 KB
testcase_93 AC 2 ms
4,376 KB
testcase_94 AC 1 ms
4,376 KB
testcase_95 AC 1 ms
4,380 KB
testcase_96 AC 1 ms
4,376 KB
testcase_97 AC 2 ms
4,380 KB
testcase_98 AC 1 ms
4,376 KB
testcase_99 AC 2 ms
4,376 KB
testcase_100 AC 1 ms
4,376 KB
testcase_101 AC 2 ms
4,376 KB
testcase_102 AC 2 ms
4,376 KB
testcase_103 AC 1 ms
4,376 KB
testcase_104 AC 1 ms
4,376 KB
testcase_105 AC 1 ms
4,376 KB
testcase_106 AC 2 ms
4,380 KB
testcase_107 AC 1 ms
4,376 KB
testcase_108 AC 2 ms
4,376 KB
testcase_109 AC 2 ms
4,376 KB
testcase_110 AC 1 ms
4,376 KB
testcase_111 AC 2 ms
4,376 KB
testcase_112 AC 1 ms
4,376 KB
testcase_113 AC 2 ms
4,376 KB
testcase_114 AC 2 ms
4,376 KB
testcase_115 AC 2 ms
4,380 KB
testcase_116 AC 2 ms
4,380 KB
testcase_117 AC 2 ms
4,376 KB
testcase_118 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

const ll INF = 1010101010101010101LL;
const ll mod = 1000000007;

ll mod_pow(ll x, ll n)
{
    ll pw[63];
    pw[0]=x;
    for(int i=1; i<63; ++i) pw[i]=(pw[i-1]*pw[i-1])%mod;

    ll ret=1;
    rep(i,63)
    {
        if(n>>i&1) (ret*=pw[i])%=mod;
    }
    return ret;
}

ll calc_length(int k)
{
    ll n=1;
    for(ll i=2; i<=k; ++i)
    {
        ll s=i*i;

        int ct=0;
        while(s>0)
        {
            s/=10;
            ++ct;
        }

        n = 2*n+ct;

        if(n>INF) return INF;
    }
    return n;
}

// 数列の要素数
ll calc_size(int K)
{
    ll ret=1;
    for(int i=2; i<=K; ++i)
    {
        ret = 2*ret+1;
        if(ret>INF) return INF;
    }
    return ret;
}

// 数列のindex番目までを文字にした時の文字の長さ
ll len(ll index, int K)
{
    ll ret=0;

    ll d=1;
    for(ll i=1; i<=K; ++i)
    {
        ll sq=i*i;
        ll l=0;
        while(sq>0)
        {
            sq/=10;
            ++l;
        }

        ll num = (index+d)/(d*2);
        if(num<0) num=0;

        ret += l*num;

        d*=2;
        if(d>index) break;
    }

    return ret;
}

void solve()
{
    int K;
    ll L,R;
    cin >>K >>L >>R;

    if(calc_length(K)<R)
    {
        printf("-1\n");
        return;
    }

    // 以下、解が存在する場合
    if(K==1)
    {
        printf("1 1\n");
        return;
    }

    // K>=2
    ll ans1=0, ans2=1;

    // 初めてL以上になるインデックスprを探す
    ll pl=0,pr=calc_size(K);
    while(pr-pl>1)
    {
        ll m=(pl+pr)/2;
        if(len(m,K)>=L) pr=m;
        else pl=m;
    }

    // 初めてR以下になるインデックスqlを探す
    ll ql=0,qr=calc_size(K)+1;
    while(qr-ql>1)
    {
        ll m=(ql+qr)/2;
        if(len(m,K)<=R) ql=m;
        else qr=m;
    }

    // printf("pr,ql (%lld, %lld)\n", pr,ql);

    // インデックスpr+1 ~ qlは全体が含まれるので、間を計算
    ll d=1;
    for(ll i=1; i<=K; ++i)
    {
        ll num = (ql+d)/(d*2) - (pr+1+d-1)/(d*2);
        if(num<0) num=0;

        ll sq=i*i;

        ll tsum=0;
        ll tprod=1;
        while(sq>0)
        {
            int v=sq%10;
            if(v==0) v=10;

            tsum+=v;
            (tprod*=v)%=mod;

            sq/=10;
        }

        ans1 += tsum*num;
        if(i>1)
        {
            ans2 *= mod_pow(tprod,num);
            ans2 %= mod;
        }
        d*=2;
        if(d>INF) break;
    }

    if(pr<=ql)
    {
        // インデックスpr
        ll div_count=1;
        ll tmp=pr;
        while(tmp%2==0)
        {
            tmp/=2;
            ++div_count;
        }
        string s=to_string(div_count * div_count);
        ll components = len(pr,K)-L+1;

        // cout << "pr:s = " << s << endl;
        // printf("components = %lld\n", components);

        int idx=s.size()-1;
        while(components>0)
        {
            int v=s[idx]-'0';
            if(v==0) v=10;

            ans1+=v;
            (ans2*=v)%=mod;

            --idx;
            --components;
        }

        // インデックスql+1
        // まずql+1が存在するか確認
        if(ql+1<=calc_size(K))
        {
            div_count=1;
            tmp=ql+1;
            while(tmp%2==0)
            {
                tmp/=2;
                ++div_count;
            }
            s=to_string(div_count * div_count);
            components = R-len(ql,K);

            // cout << "ql+1:s = " << s << endl;
            // printf("components = %lld\n", components);

            idx=0;
            while(components>0)
            {
                int v=s[idx]-'0';
                if(v==0) v=10;

                ans1+=v;
                (ans2*=v)%=mod;

                ++idx;
                --components;
            }
        }
    }
    else
    {
        ll div_count=1;
        ll tmp=pr;
        while(tmp%2==0)
        {
            tmp/=2;
            ++div_count;
        }
        string s=to_string(div_count * div_count);

        ll pre = len(ql,K);
        for(ll i=L; i<=R; ++i)
        {
            int v=s[i-pre]-'0';
            if(v==0) v=10;

            ans1+=v;
            (ans2*=v)%=mod;
        }
    }

    printf("%lld %lld\n", ans1, ans2);
}

void test()
{
    for(int k=1; k<=100000000; ++k)
    {
        printf("k = %d , len = %lld\n", k, calc_length(k));
        if(calc_length(k)>1000000000000000000LL) break;
    }
}

int main()
{
    // test();
    solve();
    return 0;
}
0