QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#183067#5739. Super Meat Brosucup-team1209AC ✓2208ms114624kbC++1411.1kb2023-09-18 21:57:542023-09-18 21:57:55

Judging History

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

  • [2023-09-18 21:57:55]
  • 评测
  • 测评结果:AC
  • 用时:2208ms
  • 内存:114624kb
  • [2023-09-18 21:57:54]
  • 提交

answer

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;

template<class T = double>
struct __Complex {
    T x, y;
    __Complex() = default;
    __Complex(const T& x, const T& y) : x(x), y(y) {}
    __Complex& operator+=(const __Complex& b) {
        x += b.x;
        y += b.y;
        return *this;
    }
    __Complex& operator-=(const __Complex& b) {
        x -= b.x;
        y -= b.y;
        return *this;
    }
    __Complex& operator*=(const __Complex& b) {
        __Complex temp = *this;
        temp.x = x * b.x - y * b.y;
        temp.y = x * b.y + y * b.x;
        *this = temp;
        return *this;
    }
    __Complex& operator*=(const double& b) {
        this -> x *= b;
        this -> y *=b;
        return *this;
    }
    __Complex operator+(const __Complex& b) {
        __Complex a = *this;
        a += b;
        return a;
    }
    __Complex operator-(const __Complex& b) {
        __Complex a = *this;
        a -= b;
        return a;
    }
    __Complex operator*(const __Complex& b) {
        __Complex a = *this;
        a *= b;
        return a;
    }
    __Complex conj() {
    	return __Complex(x, -y);
    }
    friend ostream& operator<<(ostream& os, const __Complex& a) {
        os << a.x << " " << a.y;
        return os;
    }
};
using Complex = __Complex<>;
const long double PI = acos(-1.0);
const long double PI2 = PI / 2;
const int CONQUER_BIT = 16;
const int CONQUER_MASK = (1 << CONQUER_BIT) - 1;
const Complex I(0, 1);
vector<Complex> r;
int preLg;
void pre(const int lg) {
    r.resize(1 << lg);
    for (int i = preLg ; i < lg ; i++) {
    	int L = 1 << i;
    	r[L] = Complex(cos(PI2 / L), sin(PI2 / L));
    	for (int j = L + 1 ; j < (L << 1) ; j++) {
    		r[j] = r[j - L] * r[L];
    	}
    }
}
template<class T> long long Tint2(const T val, const int mod) {
	long long v = val;
	return ((v < 0) ? (mod + (v - 1) / 2 % mod) : (v + 1) / 2) % mod;
}
template<class T> struct Poly {
    vector<T> a;
    Poly(const int size) {
        a.resize(size);
    }
    T &operator[](const int x) {
        return a[x];
    }
    void resize(const int n) {
        a.resize(n);
    }
    int size() {
        return a.size();
    }
    void FFT() {
 		int n = a.size();
 		for (int i = n ; i >= 2 ; i >>= 1) {
 			int L = i >> 1;
 			for (int j = 0 ; j != L ; j++) {
 				Complex x = a[j], y = a[j + L];
 				a[j] = x + y;
 				a[j + L] = x - y;
 			}
 			for (int j = i, m = 1 ; j != n ; j += i, m++) {
 				Complex rt = r[m];
 				for (int k = 0 ; k != L ; k++) {
 					Complex x = a[j + k], y = a[j + k + L] * rt;
 					a[j + k] = x + y;
 					a[j + k + L] = x - y;
 				}
 			}
 		}
    }
    void IFFT() {
    	int n = a.size();
    	for (int i = 2 ; i <= n ; i <<= 1) {
    		int L = i >> 1;
    		for (int j = 0 ; j != L ; j++) {
    			Complex x = a[j], y = a[j + L];
 				a[j] = x + y;
 				a[j + L] = x - y;
    		}
    		for (int j = i, m = 1 ; j != n ; j += i, m++) {
 				Complex rt = r[m];
 				for (int k = 0 ; k != L ; k++) {
 					Complex x = a[j + k], y = a[j + k + L];
 					a[j + k] = x + y;
 					a[j + k + L] = (x - y) * rt;
 				}
 			}
    	}
    	double inv = 1.0 / n;
    	for (int i = 0 ; i < n ; i++) {
    		a[i] *= inv;
    	}
    	reverse(begin(a) + 1, end(a));
    }
    void mul(Poly &x, const int mod) {
    	int n = 1, u = a.size(), m = x.size(), lg = 0, len = u + m - 1;
        while (n < len) {
            n <<= 1;
            lg++;
        }
        if (lg > preLg) {
            pre(lg);
            preLg = lg;
        }
        Poly<Complex> P(n), Q(n);
        for (int i = 0 ; i < u ; i++) {
        	P[i] = Complex(a[i] & CONQUER_MASK, a[i] >> CONQUER_BIT);
        }
    	P.FFT();
        Poly<Complex> _Q(P);
        for (int i = 0 ; i < m ; i++) {
    		Q[i] = Complex(x[i] & CONQUER_MASK, x[i] >> CONQUER_BIT);
        }
        Q.FFT();
        P[0] *= Q[0].x * 2;
        _Q[0] *= Q[0].y * 2;
        for (int d = 0 ; d < lg ; d++) {
        	int L = 1 << d, msk = L - 1;
        	for (int i = L ; i < (L << 1) ; i++) {
        		Complex &p = Q[i], q = Q[i ^ msk].conj();
        		Complex a = (p + q), b = (q - p) * I;
        		P[i] *= a;
        		_Q[i] *= b;
        	}
        }
    	P.IFFT();
    	_Q.IFFT();
    	resize(len);
    	for (int i = 0 ; i < len ; i++) {
    		long long cur = (Tint2(_Q[i].y, mod) << (CONQUER_BIT << 1))
    		 	+ (Tint2(_Q[i].x + P[i].y, mod) << CONQUER_BIT)
    		 	+ (Tint2(P[i].x, mod));
    		 a[i] = cur % mod;
    	}
    }
}; // Poly


const int mod = 1e9 + 9; 
int add(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b; 
}
int dec(int a, int b) {
    return a - b < 0 ? a - b + mod : a - b; 
}
int mul(int a, int b) {
    return 1ll * a * b % mod;
}
void Add(int &a, int b) {
    a = add(a, b);
}
void Dec(int &a, int b) {
    a = dec(a, b);
}
void Mul(int &a, int b) {
    a = mul(a, b);
}
int ksm(int a, int b) {
    int c = 1; 
    for(; b; b >>= 1, Mul(a, a))
    if(b & 1) Mul(c, a);
    return c; 
}

namespace poly {
	using u64 = unsigned long long;
    std :: vector <int> conv(std :: vector <int> P, std :: vector <int> Q) {
        if(P.size() == 0 || Q.size() == 0) return {};
        Poly<int> p(P.size());
        for(int i = 0; i < P.size(); i++)
            p[i] = P[i];
        Poly<int> q(Q.size());
        for(int i = 0; i < Q.size(); i++)
            q[i] = Q[i];
        p.mul(q, mod);
        P.clear();
        for(int i = 0; i < p.size(); i++)
            P.pb(p[i]);
        return P; 
    }
    std :: vector <int> inv(std :: vector <int> a, int n) {
        int l = 1; 
        for(; l < n; l <<= 1);
        std :: vector <int> b(l), c, d;
        a.resize(l);
        b[0] = ksm(a[0], mod - 2);
        for(int r = 2; r <= l; r <<= 1){
            c.resize(r);
            d.resize(r);
            for(int i = 0; i < r; i++) c[i] = a[i];
            for(int i = 0; i < (r >> 1); i++) d[i] = b[i];
            c = conv(c, d);
            for(int i = 1; i < (r >> 1); i++) c[i]=0;
            c[0] = mod - 1;
            d = conv(c, d);
            for(int i = r >> 1; i < r; i++) b[i] = (mod - d[i]) % mod;
        }
        b.resize(n);
        return b;
    }

    vector <int> integ(vector <int> f){
        f.push_back(0);
        for(int i = (int)f.size()-2; ~i; i--) f[i + 1] = mul(ksm(i + 1, mod - 2), f[i]);
        f[0] = 0; return f;
    }
    vector <int> deriv(vector <int> f){
        for(int i = 1; i < (int)f.size(); i++) f[i-1] = mul(f[i], i); f.pop_back(); 
        return f;
    }
    vector <int> ln(vector <int> f, int deg){ 
        vector <int> g = integ(conv(deriv(f), inv(f, deg))); 
        g.resize(deg); return g; 
    }
    vector <int> exp(vector <int> a, int deg){
        vector <int> b(1, 1), c; a.resize(deg << 1);
        for(int lim = 2; lim < (deg << 1); lim <<= 1){
            c = ln(b, lim);
            for(int i = 0; i < lim; i++) assert(i < c.size()) , c[i] = dec(a[i], c[i]);
            Add(c[0], 1); b = conv(b, c); b.resize(lim);
        } b.resize(deg); return b;
    }
    
	int eval(std::vector<int> P, std::vector<int> Q, int m) {
		if(m == 0) {
			return P[0];
		}
		std::vector<int> negQ = Q;
		for(int i = 1;i < (int) negQ.size();i += 2) {
			negQ[i] = (mod - negQ[i]) % mod;
		}
		P = conv(P, negQ);
		Q = conv(Q, negQ);
		std::vector<int> remP, remQ;
		for(int i = 0;i < (int) Q.size();i += 2) remQ.push_back(Q[i]);
		for(int i = m % 2;i < (int) P.size();i += 2) remP.push_back(P[i]);
		return eval(remP, remQ, m / 2);
	}
}
cs int N = 1e5 + 5; 

int n, m;

const int p = mod; 
vector<int> berlekamp_massey(const vector<int> &a) {
    vector<int> v, last; // v is the answer, 0-based
    int k = -1, delta = 0;
    for (int i = 0; i < (int)a.size(); i++) {
        int tmp = 0;
        for (int j = 0; j < (int)v.size(); j++)
            tmp = (tmp + (long long)a[i - j - 1] * v[j]) % p;
        if (a[i] == tmp) continue;
        if (k < 0) {
            k = i; delta = (a[i] - tmp + p) % p;
            v = vector<int>(i + 1); continue; 
        }
        vector<int> u = v;
        int val = (long long)(a[i] - tmp + p) * ksm(delta, p - 2) % p;
        if (v.size() < last.size() + i - k)
            v.resize(last.size() + i - k);
        (v[i - k - 1] += val) %= p;
        for (int j = 0; j < (int)last.size(); j++) {
            v[i - k + j] = (v[i - k + j] - (long long)val * last[j]) % p;
        if (v[i - k + j] < 0) v[i - k + j] += p; }
        if ((int)u.size() - i < (int)last.size() - k) {
            last = u; k = i; delta = a[i] - tmp;
            if (delta < 0) delta += p; 
        } 
    }
    for (auto &x : v) x = (p - x) % p;
    v.insert(v.begin(), 1); //一般是需要最小递推式的, 处理一下
    return v; 
}

int main() { 
    #ifdef zqj 
    freopen("1.in","r",stdin);
    #endif

    cin >> n >> m; 
    int d = n * n; 

    
    vector <int> a(n + 1), b(n + 1);
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    for(int i = 1; i <= n; i++)
        cin >> b[i];
    vector <int> fc(d + 1), ifc(d + 1);
    fc[0] = ifc[0] = 1; 
    for(int i = 1; i <= d; i++)
        fc[i] = mul(fc[i - 1], i);
    ifc[d] = ksm(fc[d], mod - 2);
    for(int i = d - 1; i; i--)
        ifc[i] = mul(ifc[i + 1], i + 1);

    // vector<int> check = poly :: ln({1, mod - 1}, 5);
    // vector<int> iv = poly :: inv({1, mod - 1}, 10);
    // for(int x : check) cout << x<< ' '; cout <<endl;
    // check = poly :: exp(check, 5);
    // for(int x : check) cout << x<< ' '; cout <<endl;
    // for(int x : iv) cout << x<< ' '; cout <<endl;

    vector <int> aa(n + 1), bb(n + 1);
    aa[0] = bb[0] = 1; 
    for(int i = 1; i <= n; i++) aa[i] = mod - a[i], bb[i] = mod - b[i];
    vector <int> ff = poly :: inv(aa, d + 1);
    vector <int> gg = poly :: inv(bb, d + 1);
    
    for(int i = 0; i <= d; i++)
        Mul(ff[i], ifc[i]), Mul(gg[i], ifc[i]);
    ff = poly :: conv(ff, gg);
    for(int i = 0; i <= d; i++)
        Mul(ff[i], fc[i]);
    ff.resize(d + 1); 

    aa = poly :: ln(aa, d + 1);
    bb = poly :: ln(bb, d + 1);
    aa[0] = bb[0] = n;  

    for(int i = 1; i <= d; i++)
        Mul(aa[i], mod - ifc[i - 1]);
    for(int i = 1; i <= d; i++)
        Mul(bb[i], mod - ifc[i - 1]);
    
    vector <int> c = poly :: conv(aa, bb);
    c.resize(d + 1);
    for(int i = 1; i <= d; i++)
        Mul(c[i], mod - fc[i - 1]);
    c[0] = 0; 
    c = poly :: exp(c, d + 1);

    vector <int> up = poly :: conv(ff, c);
    up.resize(d + 1);
    int ans = poly :: eval(up, c, m);
    cout << ans << endl;

}
//     for(int i = 0; i <= d; i++) cout << f[i] << ' '; cout << endl;
//     vector <int> v = berlekamp_massey(f); 
//     cout << "fasfsdasd " << v.size() << endl;
//     for(int x : v)cout << x << ' '; cout << endl;
//     vector <int> vvv = poly :: inv(v, d + 1);
//     for(int x : vvv) cout << x<< ' '; cout << endl;
//     std :: vector <int> fff = poly :: inv(f, n * n + 1);
//     for(int i = 0; i < fff.size(); i++) cout << fff[i] << ' '; cout << endl;
//     cout << ans << '\n'; 
// }

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3836kb

input:

2 3
1 1
1 1

output:

18

result:

ok 1 number(s): "18"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3652kb

input:

3 4
1 2 3
1 3 2

output:

180

result:

ok 1 number(s): "180"

Test #3:

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

input:

2 10000
1 1
1 1

output:

219175682

result:

ok 1 number(s): "219175682"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3640kb

input:

3 10000
1 2 3
1 3 2

output:

22506633

result:

ok 1 number(s): "22506633"

Test #5:

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

input:

2 100000
1 1
1 1

output:

545932818

result:

ok 1 number(s): "545932818"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3872kb

input:

3 100000
1 2 3
1 3 2

output:

57531357

result:

ok 1 number(s): "57531357"

Test #7:

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

input:

2 1000000
1 1
1 1

output:

573543093

result:

ok 1 number(s): "573543093"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

3 1000000
1 2 3
1 3 2

output:

403805847

result:

ok 1 number(s): "403805847"

Test #9:

score: 0
Accepted
time: 113ms
memory: 8964kb

input:

100 10
611820018 75283856 260526347 643767967 631268284 65648470 256718930 106368182 843661377 781313189 595194538 910990296 902258405 714186458 994234477 224492449 18296975 46262610 20991790 104288095 566832326 611425812 406579987 77488677 292658024 472980409 639810931 869525405 785145 7334819 9037...

output:

398533280

result:

ok 1 number(s): "398533280"

Test #10:

score: 0
Accepted
time: 130ms
memory: 9960kb

input:

100 1000
337861296 954983610 933984767 158413395 236083015 753884779 480227722 932008977 222135711 538427797 248038326 483405605 988416942 772990315 109612622 13993552 741972144 431449344 885517243 122926909 624917994 844993934 728622505 562105730 55499197 550328547 339918311 264032815 950626134 179...

output:

73138730

result:

ok 1 number(s): "73138730"

Test #11:

score: 0
Accepted
time: 201ms
memory: 13488kb

input:

100 1000000
903000973 111290548 476765164 551975212 417263983 767801442 332671705 278870842 201796633 763068216 960266344 26482099 425400417 95404984 465717382 564861093 884664057 821929061 592534023 30343744 936134428 561670930 15703251 993624219 870390001 723109936 981318197 656667259 663611325 79...

output:

184586019

result:

ok 1 number(s): "184586019"

Test #12:

score: 0
Accepted
time: 217ms
memory: 16780kb

input:

100 1000000000
604297456 257710428 680778188 39374726 220956130 149356685 838936858 237228921 460651102 14915933 281581197 546327552 891296342 578540918 257819754 952356650 464523259 475942266 855600625 501203770 866723687 956420345 653224524 350671741 193934342 737912261 157584420 514316163 5731646...

output:

319681565

result:

ok 1 number(s): "319681565"

Test #13:

score: 0
Accepted
time: 43ms
memory: 5380kb

input:

47 939103
460209796 923769396 497130455 808750267 18580352 834685802 844939762 655958719 739484445 425067482 799235401 220463915 346712582 882586918 650792581 69525904 386758289 448868866 132258056 596695126 476338507 436189171 608073202 431881768 654354303 965133883 322147241 729417140 303007941 43...

output:

697877594

result:

ok 1 number(s): "697877594"

Test #14:

score: 0
Accepted
time: 1ms
memory: 3856kb

input:

5 20320
382406383 333353114 214385221 941605706 957498024
619083842 292772465 335037867 449241457 284312462

output:

648153718

result:

ok 1 number(s): "648153718"

Test #15:

score: 0
Accepted
time: 1ms
memory: 3888kb

input:

10 203
113356422 444048748 334760365 168456429 484325461 304651414 551964556 490305389 320866640 344214227
59919841 828724485 840698445 104730082 572733049 659837626 200222587 316651460 461116205 929846700

output:

860154954

result:

ok 1 number(s): "860154954"

Test #16:

score: 0
Accepted
time: 163ms
memory: 11604kb

input:

100 23203
590215812 330919503 361804049 160841393 202577205 767474814 261396779 534556107 11546512 315928375 548186686 418841505 971313703 728695556 991214364 553240798 294967304 944071947 960112453 266119259 515272798 476117665 854261275 247679517 137587630 103865755 561276374 970920554 9978160 549...

output:

319899104

result:

ok 1 number(s): "319899104"

Test #17:

score: 0
Accepted
time: 51ms
memory: 6264kb

input:

47 1000000000
661236701 626011023 653401016 269507301 947858324 488679806 682307210 795514627 747127946 933364412 126186059 532060957 912405701 875433550 878553905 363499560 534441266 584528443 703237317 73856741 96530866 645865395 853378035 916787403 685813713 118694785 912869318 420101396 63662492...

output:

975440921

result:

ok 1 number(s): "975440921"

Test #18:

score: 0
Accepted
time: 232ms
memory: 15116kb

input:

147 10
462565524 147218320 961380299 517841640 474426942 690678806 708345902 138526412 594824170 711085626 400440944 547048987 257456629 858639492 731548623 743813006 821987881 212909550 986929898 302432282 797126565 348883326 723121207 65669843 523779740 462686631 179539669 863701865 324988545 8236...

output:

842067007

result:

ok 1 number(s): "842067007"

Test #19:

score: 0
Accepted
time: 667ms
memory: 36720kb

input:

250 1000
359444936 577808352 826049210 937279560 968237772 846436826 63317947 327295422 805344944 972799416 11317310 147013950 783864555 646400672 181338678 200051004 380780464 782975995 907350103 984833619 119607074 682306803 697289898 147681694 652404823 348889915 901738893 696974445 693347094 616...

output:

441591881

result:

ok 1 number(s): "441591881"

Test #20:

score: 0
Accepted
time: 831ms
memory: 44296kb

input:

200 1000000
874654868 716961475 190056783 217613512 270419311 928606069 110795081 797619047 5066043 622162200 233468286 849682819 932605405 421878748 971561346 522911058 820676673 541104759 868007035 915383864 505494377 249870500 843072321 537442117 621304223 279155014 364150264 753394165 586053537 ...

output:

718830226

result:

ok 1 number(s): "718830226"

Test #21:

score: 0
Accepted
time: 1022ms
memory: 54020kb

input:

190 1000000000
292432717 113243080 782850858 172875356 666459177 694100208 895717171 654232213 811149478 692945443 132670383 501884040 952942129 647574489 801625042 7020933 497251380 57436006 898832679 504715288 374768882 690448784 733992082 836263798 169618991 932677069 868515629 488357250 6225605 ...

output:

262821982

result:

ok 1 number(s): "262821982"

Test #22:

score: 0
Accepted
time: 1142ms
memory: 49812kb

input:

300 10
141416754 840193279 939495438 894074444 357961599 893355088 946771006 468551335 988010195 578425753 45582485 678562805 842752189 299012071 877195300 942962308 312940913 48091926 593525589 431117014 585154959 534766048 914518145 306770747 91708675 676599371 136242034 913227941 655846387 546182...

output:

477817221

result:

ok 1 number(s): "477817221"

Test #23:

score: 0
Accepted
time: 1376ms
memory: 58060kb

input:

300 1000
881842413 86529938 693226698 20757197 164509906 310379730 597581323 380048694 772704893 160215562 406792873 97226450 513243503 324045599 972495119 257440375 826958761 280401032 425640175 326986758 420258228 298452017 710062261 224951134 491316417 407531361 26721586 472379648 484051640 16798...

output:

732490438

result:

ok 1 number(s): "732490438"

Test #24:

score: 0
Accepted
time: 1792ms
memory: 86244kb

input:

300 1000000
100579760 892289966 263679968 936720216 747697319 399346310 126202533 776869874 580862239 418953789 573536832 409884862 171308486 98937954 131377655 991658975 439071483 512301561 944105267 932299071 894863189 263184189 66678588 477820447 34975336 982457276 71778919 387114327 308627278 20...

output:

678269002

result:

ok 1 number(s): "678269002"

Test #25:

score: 0
Accepted
time: 2208ms
memory: 114624kb

input:

300 1000000000
844189155 306246728 764486702 296212354 682237724 995426531 484985870 919411723 19081297 425060640 679557693 229668380 370315094 453252907 409487440 383346311 304432744 706524934 456058788 261585079 888251458 726586849 720010611 433565812 778508674 751679601 496382059 474420983 254876...

output:

255690783

result:

ok 1 number(s): "255690783"