QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431472#1285. Stirling NumberRong7AC ✓64ms43144kbC++143.5kb2024-06-05 17:02:032024-06-05 17:02:03

Judging History

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

  • [2024-06-05 17:02:03]
  • 评测
  • 测评结果:AC
  • 用时:64ms
  • 内存:43144kb
  • [2024-06-05 17:02:03]
  • 提交

answer

// Not afraid to dark.

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

clock_t start_time, end_time;
#define GET_START start_time = clock ();
#define GET_END end_time = clock (); fprintf (stderr, "TIME COSSEMED : %0.3lf\n", 1.0 * (end_time - start_time) / CLOCKS_PER_SEC);
#define inline __inline__ __attribute__ ((always_inline))

#define int long long

namespace io {
    int read_pos, read_dt; char read_char;
    inline int read (int &p = read_pos){
        p = 0, read_dt = 1; read_char = getchar ();
        while (! isdigit (read_char)){
            if (read_char == '-')
                read_dt = - 1;
            read_char = getchar ();
        }
        while (isdigit (read_char)){
            p = (p << 1) + (p << 3) + read_char - 48;
            read_char = getchar ();
        }
        return p = p * read_dt;
    }
    int write_sta[65], write_top;
    inline void write (int x){
        if (x < 0)
            putchar ('-'), x = - x;
        write_top = 0;
        do
            write_sta[write_top ++] = x % 10, x /= 10;
        while (x);
        while (write_top)
            putchar (write_sta[-- write_top] + 48);
    }
}

const int P = 1e6;
int p, g, gp[P + 5];
int f[P + 5]; // f[i]=f(g^i)
int jc[P + 5], inv[P + 5], inj[P + 5];

#define vic(x) (((x) & 1) ? (p - 1) : 1)

inline int power (int a, int t){
    int res = 1;
    while (t > 0){
        if (t & 1)
            res = res * a % p;
        a = a * a % p;
        t >>= 1;
    }
    return res;
}

inline void Init (int n){
    vector < int > fac;
    jc[0] = jc[1] = inv[1] = inj[0] = inj[1] = 1;
    for (int i = 2, rs = p - 1;i < p;++ i){
        inv[i] = inv[i - p % i] * (p / i + 1) % p;
        jc[i] = jc[i - 1] * i % p;
        inj[i] = inj[i - 1] * inv[i] % p;
        if (rs % i == 0){
            while (rs % i == 0)
                rs /= i;
            fac.push_back (i);
        }
        if (i == p - 1 && rs > 1)
            fac.push_back (rs);
    }
    for (int i = 2;i < p;++ i){
        for (int x : fac)
            if (power (i, (p - 1) / x) == 1)
                goto out_case;
        g = i;
        break;
        out_case : ;
    }
    gp[0] = 1;
    for (int i = 1;i < p;++ i)
        gp[i] = gp[i - 1] * g % p;
    for (int i = 0;i < p;++ i)
        f[i] = ((gp[i] + n - 1 < p) ? (jc[gp[i] + n - 1] * inj[gp[i] - 1] % p) : 0);
}

inline int Sum (int n, int m){
    if (n == 0)
        return 1;
    m = min (n, m);
    int res = (m + 1) * f[0] % p;
    for (int i = 1;i < p - 1;++ i)
        res = (res + f[i] * (gp[p - 1 - i * (m + 1) % (p - 1)] - 1) % p * inv[gp[p - 1 - i] - 1] % p) % p;
    return (p - res) % p;
}

int C (int a, int b){
    if (a < 0 || b < 0)
        return 0;
    if (a < b)
        return 0;
    if (a >= p)
        return C (a / p, b / p) * C (a % p, b % p) % p;
    return jc[a] * inj[a - b] % p * inj[b] % p;
}

inline int Calc (int n, int m){
    if (m < 1)
        return 0;
    if (n == m){
        if (n >= p)
            return 0;
        return jc[n];
    }
    int q = n / p, r = n % p;
    if (m < q)
        return 0;
    m -= q;
    int k = m / (p - 1), t = m % (p - 1);
    return vic (q - k) * (C (q, k) * Sum (r, t) % p - C (q - 1, k - 1) * jc[r] % p + p) % p;
}

inline void ARKNIGHTS (){
    int n = io::read (), l = io::read (), r = io::read (); io::read (p);
    Init (n % p);
    io::write ((Calc (n, r) - Calc (n, l - 1) + p) % p), puts ("");
}

signed main (){
    GET_START

    ARKNIGHTS ();

    GET_END
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 12076kb

input:

4 1 4 5

output:

4

result:

ok "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 12076kb

input:

6 5 5 29

output:

15

result:

ok "15"

Test #3:

score: 0
Accepted
time: 57ms
memory: 42784kb

input:

1000 685 975 999983

output:

482808

result:

ok "482808"

Test #4:

score: 0
Accepted
time: 2ms
memory: 11984kb

input:

8 7 8 7

output:

1

result:

ok "1"

Test #5:

score: 0
Accepted
time: 2ms
memory: 12156kb

input:

6 4 6 3

output:

2

result:

ok "2"

Test #6:

score: 0
Accepted
time: 0ms
memory: 11924kb

input:

31494923 16387579 27674098 2

output:

0

result:

ok "0"

Test #7:

score: 0
Accepted
time: 25ms
memory: 27440kb

input:

1000000000 179971578 813833436 383101

output:

53093

result:

ok "53093"

Test #8:

score: 0
Accepted
time: 14ms
memory: 24764kb

input:

1000000000 243662537 919841454 336437

output:

75332

result:

ok "75332"

Test #9:

score: 0
Accepted
time: 62ms
memory: 43060kb

input:

1000000000 802415407 880806868 999983

output:

960771

result:

ok "960771"

Test #10:

score: 0
Accepted
time: 62ms
memory: 42936kb

input:

1000000000 644768002 859679621 999983

output:

805072

result:

ok "805072"

Test #11:

score: 0
Accepted
time: 59ms
memory: 42792kb

input:

1000000000 413491669 805704689 999983

output:

138470

result:

ok "138470"

Test #12:

score: 0
Accepted
time: 49ms
memory: 42936kb

input:

48537068788 22847195743 28163317559 999983

output:

529264

result:

ok "529264"

Test #13:

score: 0
Accepted
time: 64ms
memory: 43060kb

input:

828536325370 779765412000 782633091631 999983

output:

836701

result:

ok "836701"

Test #14:

score: 0
Accepted
time: 61ms
memory: 42940kb

input:

258077969836231211 200983000620029238 226661348290193221 999983

output:

104488

result:

ok "104488"

Test #15:

score: 0
Accepted
time: 55ms
memory: 42988kb

input:

258904208719347339 185679775210965354 223112834501603079 999983

output:

0

result:

ok "0"

Test #16:

score: 0
Accepted
time: 61ms
memory: 43056kb

input:

259730451897430763 53367210716367086 159749126385022258 999983

output:

0

result:

ok "0"

Test #17:

score: 0
Accepted
time: 55ms
memory: 42812kb

input:

260556695075514187 28149045695465635 29562653808086859 999983

output:

669091

result:

ok "669091"

Test #18:

score: 0
Accepted
time: 48ms
memory: 42220kb

input:

1000000000000000000 199156813867768126 571262092911942493 919337

output:

732102

result:

ok "732102"

Test #19:

score: 0
Accepted
time: 60ms
memory: 42772kb

input:

353534534534 1999983 2234324324 999983

output:

613376

result:

ok "613376"

Test #20:

score: 0
Accepted
time: 47ms
memory: 43144kb

input:

353534534534 0 2234324324 999983

output:

520965

result:

ok "520965"

Test #21:

score: 0
Accepted
time: 48ms
memory: 43072kb

input:

353534534534 22342343 353534534534 999983

output:

281927

result:

ok "281927"