QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#384114#2074. LCM Sumchenxinyang2006AC ✓5564ms433272kbC++147.2kb2024-04-09 20:49:532024-04-09 20:49:53

Judging History

你现在查看的是最新测评结果

  • [2024-04-09 20:49:53]
  • 评测
  • 测评结果:AC
  • 用时:5564ms
  • 内存:433272kb
  • [2024-04-09 20:49:53]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
    if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
    if(x > y) x = y;
}

inline int popcnt(int x){
    return __builtin_popcount(x);
}

inline int ctz(int x){
    return __builtin_ctz(x);
}

template <int P>
class mod_int
{
    using Z = mod_int;

private:
    static int mo(int x) { return x < 0 ? x + P : x; }

public:
    int x;
    int val() const { return x; }
    mod_int() : x(0) {}
    template <class T>
    mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
    bool operator==(const Z &rhs) const { return x == rhs.x; }
    bool operator!=(const Z &rhs) const { return x != rhs.x; }
    Z operator-() const { return Z(x ? P - x : 0); }
    Z pow(long long k) const
    {
        Z res = 1, t = *this;
        while (k)
        {
            if (k & 1)
                res *= t;
            if (k >>= 1)
                t *= t;
        }
        return res;
    }
    Z &operator++()
    {
        x < P - 1 ? ++x : x = 0;
        return *this;
    }
    Z &operator--()
    {
        x ? --x : x = P - 1;
        return *this;
    }
    Z operator++(int)
    {
        Z ret = x;
        x < P - 1 ? ++x : x = 0;
        return ret;
    }
    Z operator--(int)
    {
        Z ret = x;
        x ? --x : x = P - 1;
        return ret;
    }
    Z inv() const { return pow(P - 2); }
    Z &operator+=(const Z &rhs)
    {
        (x += rhs.x) >= P && (x -= P);
        return *this;
    }
    Z &operator-=(const Z &rhs)
    {
        (x -= rhs.x) < 0 && (x += P);
        return *this;
    }
    Z operator-()
    {
        return -x;
    }
    Z &operator*=(const Z &rhs)
    {
        x = 1ULL * x * rhs.x % P;
        return *this;
    }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                  \
    friend T operator o(const Z &lhs, const Z &rhs) \
    {                                               \
        Z res = lhs;                                \
        return res o## = rhs;                       \
    }
    setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
    
    friend istream& operator>>(istream& is, mod_int& x)
    {
        long long tmp;
        is >> tmp;
        x = tmp;
        return is;
    }
    friend ostream& operator<<(ostream& os, const mod_int& x)
    {
        os << x.val();
        return os;
    }
};

using Z = mod_int<mod>;
Z power(Z p,ll k){
    Z ans = 1;
    while(k){
        if(k % 2 == 1) ans *= p;
        p *= p;
        k /= 2;
    }
    return ans;
}
Z fact[35],ifac[35];
void init(int L){
    fact[0] = 1;
    rep(i,1,L) fact[i] = fact[i - 1] * i;
    ifac[L] = Z(1) / fact[L];
    per(i,L - 1,0) ifac[i] = ifac[i + 1] * (i + 1);
}

ll n;
int k;
const int L = 5;
const int aa[6] = {0,3,5,7,17,19},bb[6] = {0,2,11,13,23,29};
const Z ia[6] = {0,333333336,400000003,142857144,352941179,157894738},ib[6] = {0,500000004,818181824,153846155,739130440,758620695};
int _buc[6][35];
const int A = 1526175,B = 1526096;

Z ret;
void updA(int val,int C){
    rep(i,1,L){
        int rk = 0;
        while(val % aa[i] == 0){
            val /= aa[i];
            rk++;
        }
        rep(_k,1,rk){
            _buc[i][_k] += C;
            if(C > 0 && _buc[i][_k] > 1) ret *= ia[i];
            if(C < 0 && _buc[i][_k]) ret *= aa[i];
        }
    }
}

void updB(int val,int C){
    rep(i,1,L){
        int rk = 0;
        while(val % bb[i] == 0){
            val /= bb[i];
            rk++;
        }
        rep(_k,1,rk){
            _buc[i][_k] += C;
            if(C > 0 && _buc[i][_k] > 1) ret *= ib[i];
            if(C < 0 && _buc[i][_k]) ret *= bb[i];
        }
    }
}

Z a[A + 5],b[B + 5];
Z sum[2 * B + 5][35];

Z result[35],f[35],g[35];
void calc(ll u){
    fill(result,result + k + 2,0);

    int l = 0,r = 0;
    ll cur = 0;
    int idx = 0;
    rep(i,0,2 * A){
        while(1ll * A * l < cur) l++;
        while((r + 1 < B || i < A) && 1ll * A * (r + 1) - cur <= u) r++;
        if(i == A) chkmin(r,B - 1);

        rep(j,0,k + 1){
            if(l > r){
                f[j] = 0;
                continue;
            }
            f[j] = sum[r][j];
            if(l) f[j] -= sum[l - 1][j];
        }
        g[0] = a[idx];
        rep(j,1,k + 1) g[j] = g[j - 1] * (-cur + j - 1);

/*        printf("i=%d [%d,%d]\n",i,l,r);
        rep(j,0,k + 1) printf("%d ",f[j].val());
        printf("\n");
        rep(j,0,k + 1) printf("%d ",g[j].val());
        printf("\n"); */

        rep(j,0,k + 1){
            f[j] *= ifac[j];
            g[j] *= ifac[j];
        }

        rep(p,0,k + 1){
            rep(q,0,k + 1 - p) result[p + q] += f[p] * g[q];
        }

        cur += B;
        idx -= B;
        while(idx < 0) idx += A;
    }

    reverse(result,result + k + 2);
    rep(i,0,k + 1) result[i] *= ifac[i] * fact[k + 1];
/*    printf("calc u=%lld\n",u);
    rep(i,0,k + 1) printf("%d ",result[i].val());
    printf("\n");*/
    cerr << clock() << endl;
}

int main(){
//    freopen("test.in","r",stdin);
    scanf("%lld%d",&n,&k);
    init(k + 1);
    ret = 1;
    rep(i,0,k - 1) updA(A + i,1);
    rep(i,0,A){
        updA(A + i + k,1);
        if(i) updA(A + i - 1,-1);
        a[i] = ret;
    }
    ret = 1;
    memset(_buc,0,sizeof(_buc));
    rep(i,0,k - 1) updB(B + i,1);
    rep(i,0,B){
        updB(B + i + k,1);
        if(i) updB(B + i - 1,-1);
        b[i] = ret;       
    }
//    rep(i,0,A) printf("%d ",a[i].val());
//    printf("\n");
//    rep(i,0,B) printf("%d ",b[i].val());
//    printf("\n");
    cerr << clock() << endl;

    int idx = 0;
    Z cur = 0;
    rep(i,0,2 * B){
        sum[i][0] = b[idx];
        rep(j,1,k + 1) sum[i][j] = sum[i][j - 1] * (cur + j - 1);        

        idx += A;
        while(idx >= B) idx -= B;
        cur += A;
    }
    rep(i,1,2 * B){
        rep(j,0,k + 1) sum[i][j] += sum[i - 1][j];
    }
    cerr << clock() << endl;

    calc(1ll * A * B - 1);
    Z answer = 0,prd;
    ll h = 0;
    while(h + 1ll * A * B - 1 <= n){
        prd = 1;
        rep(i,0,k + 1){
            answer += result[i] * prd;
            prd *= h + i;
        }
        h += 1ll * A * B;
    }
    if(h <= n){
        calc(n - h);
        prd = 1;
        rep(i,0,k + 1){
            answer += result[i] * prd;
            prd *= h + i;
        }
    }
    printf("%d\n",answer.val());
/*    Z answer = 0,prd;
    rep(i,1,n){
        prd = 1;
        rep(j,0,k) prd *= i + j;
        answer += prd * a[i % A] * b[i % B];
    }
    printf("%d\n",answer.val());*/
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 536ms
memory: 433264kb

input:

10 3

output:

18936

result:

ok single line: '18936'

Test #2:

score: 0
Accepted
time: 804ms
memory: 433060kb

input:

10000 6

output:

43482752

result:

ok single line: '43482752'

Test #3:

score: 0
Accepted
time: 2092ms
memory: 433132kb

input:

1000000000 15

output:

688102997

result:

ok single line: '688102997'

Test #4:

score: 0
Accepted
time: 5564ms
memory: 433112kb

input:

1000000000000000000 30

output:

524223287

result:

ok single line: '524223287'

Test #5:

score: 0
Accepted
time: 429ms
memory: 433116kb

input:

1000000000000000000 1

output:

41650

result:

ok single line: '41650'

Test #6:

score: 0
Accepted
time: 494ms
memory: 433252kb

input:

1000000000000000000 2

output:

688702180

result:

ok single line: '688702180'

Test #7:

score: 0
Accepted
time: 555ms
memory: 433196kb

input:

1000000000000000000 3

output:

26803356

result:

ok single line: '26803356'

Test #8:

score: 0
Accepted
time: 641ms
memory: 433200kb

input:

1000000000000000000 4

output:

318915933

result:

ok single line: '318915933'

Test #9:

score: 0
Accepted
time: 733ms
memory: 433244kb

input:

1000000000000000000 5

output:

355484656

result:

ok single line: '355484656'

Test #10:

score: 0
Accepted
time: 836ms
memory: 433116kb

input:

1000000000000000000 6

output:

162499027

result:

ok single line: '162499027'

Test #11:

score: 0
Accepted
time: 953ms
memory: 433112kb

input:

1000000000000000000 7

output:

60587090

result:

ok single line: '60587090'

Test #12:

score: 0
Accepted
time: 1057ms
memory: 433268kb

input:

1000000000000000000 8

output:

47433228

result:

ok single line: '47433228'

Test #13:

score: 0
Accepted
time: 1196ms
memory: 433264kb

input:

1000000000000000000 9

output:

52651033

result:

ok single line: '52651033'

Test #14:

score: 0
Accepted
time: 1333ms
memory: 433208kb

input:

1000000000000000000 10

output:

488431192

result:

ok single line: '488431192'

Test #15:

score: 0
Accepted
time: 1470ms
memory: 433112kb

input:

1000000000000000000 11

output:

203359516

result:

ok single line: '203359516'

Test #16:

score: 0
Accepted
time: 1630ms
memory: 433268kb

input:

1000000000000000000 12

output:

676816954

result:

ok single line: '676816954'

Test #17:

score: 0
Accepted
time: 1772ms
memory: 433196kb

input:

1000000000000000000 13

output:

792261385

result:

ok single line: '792261385'

Test #18:

score: 0
Accepted
time: 1952ms
memory: 433256kb

input:

1000000000000000000 14

output:

266241285

result:

ok single line: '266241285'

Test #19:

score: 0
Accepted
time: 2117ms
memory: 433272kb

input:

1000000000000000000 15

output:

779954568

result:

ok single line: '779954568'

Test #20:

score: 0
Accepted
time: 2291ms
memory: 433240kb

input:

1000000000000000000 16

output:

661462563

result:

ok single line: '661462563'

Test #21:

score: 0
Accepted
time: 2479ms
memory: 433240kb

input:

1000000000000000000 17

output:

157882037

result:

ok single line: '157882037'

Test #22:

score: 0
Accepted
time: 2669ms
memory: 433056kb

input:

1000000000000000000 18

output:

707666921

result:

ok single line: '707666921'

Test #23:

score: 0
Accepted
time: 2857ms
memory: 433060kb

input:

1000000000000000000 19

output:

75350354

result:

ok single line: '75350354'

Test #24:

score: 0
Accepted
time: 3071ms
memory: 433140kb

input:

1000000000000000000 20

output:

190809078

result:

ok single line: '190809078'

Test #25:

score: 0
Accepted
time: 3281ms
memory: 433120kb

input:

1000000000000000000 21

output:

485802406

result:

ok single line: '485802406'

Test #26:

score: 0
Accepted
time: 3510ms
memory: 433180kb

input:

1000000000000000000 22

output:

518652177

result:

ok single line: '518652177'

Test #27:

score: 0
Accepted
time: 3743ms
memory: 433080kb

input:

1000000000000000000 23

output:

172946983

result:

ok single line: '172946983'

Test #28:

score: 0
Accepted
time: 3954ms
memory: 433196kb

input:

1000000000000000000 24

output:

342420888

result:

ok single line: '342420888'

Test #29:

score: 0
Accepted
time: 4212ms
memory: 433272kb

input:

1000000000000000000 25

output:

497552775

result:

ok single line: '497552775'

Test #30:

score: 0
Accepted
time: 4471ms
memory: 433056kb

input:

1000000000000000000 26

output:

920161969

result:

ok single line: '920161969'

Test #31:

score: 0
Accepted
time: 4806ms
memory: 433256kb

input:

1000000000000000000 27

output:

131607220

result:

ok single line: '131607220'

Test #32:

score: 0
Accepted
time: 4979ms
memory: 433180kb

input:

1000000000000000000 28

output:

551838958

result:

ok single line: '551838958'

Test #33:

score: 0
Accepted
time: 5243ms
memory: 433060kb

input:

1000000000000000000 29

output:

851846988

result:

ok single line: '851846988'

Test #34:

score: 0
Accepted
time: 358ms
memory: 433060kb

input:

1000000000000000000 0

output:

1225

result:

ok single line: '1225'

Test #35:

score: 0
Accepted
time: 359ms
memory: 433252kb

input:

265714758284843011 0

output:

708589

result:

ok single line: '708589'

Test #36:

score: 0
Accepted
time: 427ms
memory: 433240kb

input:

266540997167959139 1

output:

881831692

result:

ok single line: '881831692'

Test #37:

score: 0
Accepted
time: 485ms
memory: 433240kb

input:

267367244641009859 2

output:

423211036

result:

ok single line: '423211036'

Test #38:

score: 0
Accepted
time: 576ms
memory: 433040kb

input:

268193483524125987 3

output:

127124157

result:

ok single line: '127124157'

Test #39:

score: 0
Accepted
time: 657ms
memory: 433116kb

input:

269019726702209411 4

output:

39777520

result:

ok single line: '39777520'

Test #40:

score: 0
Accepted
time: 730ms
memory: 433240kb

input:

269845965585325539 5

output:

577495858

result:

ok single line: '577495858'

Test #41:

score: 0
Accepted
time: 842ms
memory: 433240kb

input:

270672213058376259 6

output:

262428469

result:

ok single line: '262428469'

Test #42:

score: 0
Accepted
time: 922ms
memory: 433208kb

input:

271498451941492387 7

output:

878988911

result:

ok single line: '878988911'

Test #43:

score: 0
Accepted
time: 1070ms
memory: 433200kb

input:

272324690824608515 8

output:

844720221

result:

ok single line: '844720221'

Test #44:

score: 0
Accepted
time: 1191ms
memory: 433132kb

input:

273150934002691939 9

output:

20339607

result:

ok single line: '20339607'

Test #45:

score: 0
Accepted
time: 1341ms
memory: 433240kb

input:

996517375802030518 10

output:

436000085

result:

ok single line: '436000085'

Test #46:

score: 0
Accepted
time: 1467ms
memory: 433120kb

input:

997343614685146646 11

output:

172229324

result:

ok single line: '172229324'

Test #47:

score: 0
Accepted
time: 1675ms
memory: 433180kb

input:

998169857863230070 12

output:

297495611

result:

ok single line: '297495611'

Test #48:

score: 0
Accepted
time: 1769ms
memory: 433264kb

input:

998996101041313494 13

output:

516501983

result:

ok single line: '516501983'

Test #49:

score: 0
Accepted
time: 1932ms
memory: 433116kb

input:

999822344219396918 14

output:

917263884

result:

ok single line: '917263884'

Test #50:

score: 0
Accepted
time: 2105ms
memory: 433196kb

input:

648583102513046 15

output:

962851231

result:

ok single line: '962851231'

Test #51:

score: 0
Accepted
time: 2268ms
memory: 433196kb

input:

1474821985629174 16

output:

635473880

result:

ok single line: '635473880'

Test #52:

score: 0
Accepted
time: 2462ms
memory: 433220kb

input:

2301069458679894 17

output:

686837493

result:

ok single line: '686837493'

Test #53:

score: 0
Accepted
time: 2653ms
memory: 433176kb

input:

3127308341796022 18

output:

746101270

result:

ok single line: '746101270'

Test #54:

score: 0
Accepted
time: 2852ms
memory: 433140kb

input:

3953551519879446 19

output:

517293260

result:

ok single line: '517293260'

Test #55:

score: 0
Accepted
time: 3092ms
memory: 433240kb

input:

738505179452405440 20

output:

96504747

result:

ok single line: '96504747'

Test #56:

score: 0
Accepted
time: 3298ms
memory: 433180kb

input:

739331418335521569 21

output:

151678490

result:

ok single line: '151678490'

Test #57:

score: 0
Accepted
time: 3532ms
memory: 433268kb

input:

740157665808572289 22

output:

548846725

result:

ok single line: '548846725'

Test #58:

score: 0
Accepted
time: 3767ms
memory: 433064kb

input:

740983904691688417 23

output:

260809011

result:

ok single line: '260809011'

Test #59:

score: 0
Accepted
time: 4005ms
memory: 433184kb

input:

741810147869771841 24

output:

62064725

result:

ok single line: '62064725'

Test #60:

score: 0
Accepted
time: 4223ms
memory: 433116kb

input:

742636386752887969 25

output:

378996555

result:

ok single line: '378996555'

Test #61:

score: 0
Accepted
time: 4487ms
memory: 433236kb

input:

743462629930971393 26

output:

749268774

result:

ok single line: '749268774'

Test #62:

score: 0
Accepted
time: 4707ms
memory: 433060kb

input:

744288873109054817 27

output:

343279726

result:

ok single line: '343279726'

Test #63:

score: 0
Accepted
time: 5002ms
memory: 433096kb

input:

745115111992170945 28

output:

275401202

result:

ok single line: '275401202'

Test #64:

score: 0
Accepted
time: 5263ms
memory: 433180kb

input:

745941355170254369 29

output:

482258407

result:

ok single line: '482258407'

Test #65:

score: 0
Accepted
time: 5550ms
memory: 433116kb

input:

257120946248004555 30

output:

871039750

result:

ok single line: '871039750'

Test #66:

score: 0
Accepted
time: 694ms
memory: 433184kb

input:

96 5

output:

821858903

result:

ok single line: '821858903'

Test #67:

score: 0
Accepted
time: 2798ms
memory: 433104kb

input:

52 19

output:

457717981

result:

ok single line: '457717981'

Test #68:

score: 0
Accepted
time: 1302ms
memory: 433240kb

input:

96 10

output:

169742074

result:

ok single line: '169742074'

Test #69:

score: 0
Accepted
time: 3873ms
memory: 433112kb

input:

37 24

output:

999563342

result:

ok single line: '999563342'

Test #70:

score: 0
Accepted
time: 1443ms
memory: 433252kb

input:

81 11

output:

38929854

result:

ok single line: '38929854'

Test #71:

score: 0
Accepted
time: 4108ms
memory: 433100kb

input:

21 25

output:

664668034

result:

ok single line: '664668034'

Test #72:

score: 0
Accepted
time: 2223ms
memory: 433204kb

input:

70 16

output:

725245983

result:

ok single line: '725245983'

Test #73:

score: 0
Accepted
time: 5429ms
memory: 433240kb

input:

22 30

output:

167544969

result:

ok single line: '167544969'

Test #74:

score: 0
Accepted
time: 1740ms
memory: 433264kb

input:

63 13

output:

743502454

result:

ok single line: '743502454'

Test #75:

score: 0
Accepted
time: 335ms
memory: 433200kb

input:

7 0

output:

28

result:

ok single line: '28'

Test #76:

score: 0
Accepted
time: 5503ms
memory: 433252kb

input:

267367244641009859 30

output:

625470986

result:

ok single line: '625470986'

Test #77:

score: 0
Accepted
time: 5481ms
memory: 433204kb

input:

268193483524125987 30

output:

55213829

result:

ok single line: '55213829'

Test #78:

score: 0
Accepted
time: 5501ms
memory: 433136kb

input:

269019726702209411 30

output:

965929889

result:

ok single line: '965929889'

Test #79:

score: 0
Accepted
time: 5558ms
memory: 433184kb

input:

269845965585325539 30

output:

87993358

result:

ok single line: '87993358'

Test #80:

score: 0
Accepted
time: 5499ms
memory: 433192kb

input:

270672213058376259 30

output:

88337416

result:

ok single line: '88337416'

Test #81:

score: 0
Accepted
time: 5509ms
memory: 433108kb

input:

271498451941492387 30

output:

753017833

result:

ok single line: '753017833'

Test #82:

score: 0
Accepted
time: 5492ms
memory: 433104kb

input:

272324690824608515 30

output:

972883027

result:

ok single line: '972883027'

Test #83:

score: 0
Accepted
time: 5506ms
memory: 433172kb

input:

273150934002691939 30

output:

644302994

result:

ok single line: '644302994'

Test #84:

score: 0
Accepted
time: 339ms
memory: 433096kb

input:

1 0

output:

1

result:

ok single line: '1'

Test #85:

score: 0
Accepted
time: 409ms
memory: 433264kb

input:

1 1

output:

2

result:

ok single line: '2'

Test #86:

score: 0
Accepted
time: 5462ms
memory: 433060kb

input:

1 30

output:

775941393

result:

ok single line: '775941393'

Test #87:

score: 0
Accepted
time: 5488ms
memory: 433136kb

input:

100 30

output:

329365290

result:

ok single line: '329365290'