QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#616521#7775. 【模板】矩阵快速幂fryan0 1055ms870728kbC++235.1kb2024-10-06 07:05:262024-10-06 07:05:27

Judging History

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

  • [2024-10-06 07:05:27]
  • 评测
  • 测评结果:0
  • 用时:1055ms
  • 内存:870728kb
  • [2024-10-06 07:05:26]
  • 提交

answer

#pragma GCC optimize("O3,unroll-loops")
#include "bits/stdc++.h"
using namespace std;
#define i32 int32_t
#define int int64_t
#define i128 __int128_t
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

std::ostream& operator<<(std::ostream& out, __int128 value) { char buffer[40], *ptr = &buffer[39]; *ptr = '\0'; bool neg = value < 0; if (neg) value = -value; do { *--ptr = '0' + (value % 10); value /= 10; } while (value); if (neg) *--ptr = '-'; return out << ptr; }

struct bigint {
	static const int B = 1e18;
	static const int L = 5;
	i128 v[L];
	bigint() {memset(v,0,sizeof(v));}
	bigint(int val) {memset(v,0,sizeof(v));v[0] = val;propogate();}
	bigint(i128 val) {memset(v,0,sizeof(v));v[0] = val;		}
    bigint(const string& s) {memset(v, 0, sizeof(v));int n = s.size();int chunkSize = 18;for (int i = 0; i < n; i += chunkSize) {    int end = n - i;    int start = max((int)0, end - chunkSize);    string part = s.substr(start, end - start);    int chunk = stoll(part);    v[(i/chunkSize)] = chunk;}propogate();}
	void propogate() {for (int i=0; i<L-1; i++) {v[i+1] += v[i]/B;v[i] %= B;}}
	void add(i128 a) {v[0] += a;propogate();}
	void add(bigint b) {for (int i=0; i<L; i++) {v[i] += b.v[i];}propogate();}
	int borrowable() {	for (int i=1; i<L; i++) {if (v[i]) return 1;}	return 0;}
	void borrow(int i) {	if (!v[i+1]) borrow(v[i+1]);	v[i+1]--;	v[i] += B;}
	void subtract(int a) {	if (v[0] < a) {borrow(0);}	v[0] -= a; assert(v[0]>=0);propogate();}
	void multiply(i128 a) {	for (int i=0; i<L; i++) {v[i] *= a;}	propogate();}
	void divide(int a) {	for (int i=L-1; i>=0; i--) {		if (i) v[i-1] += B*(v[i]%a);		v[i] /= a;	}	propogate();}
	void setmin(bigint b) {	int g = 1;	for (int i=L-1; i>=0; i--) {		if (v[i] < b.v[i]) {g = 0; break;}		else if (v[i] > b.v[i]) break;	}	if (!g) return;	for (int i=L-1; i>=0; i--) {		v[i] = b.v[i];	}}
	bool lesser(i128 V) {	for (int i=1; i<L; i++) {if (v[i]) return 0;}	return (v[0]<=V);}
	void setinf() {for (int i=0; i<L; i++) {v[i] = B-1;}}
	void copy(bigint b) {for (int i=0; i<L; i++) {v[i] = b.v[i];}}
	int remainder(int d) {	i128 cr=0;	for (int i=L-1; i>=0; i--) {		cr *= B;		cr += v[i];		cr %= d;	}	return (int)cr;}
	int isinf() {	for (int i=0; i<L; i++) {		if (v[i]!=B-1) return 0;	}	return 1;}
	void print() {
	    int i = L - 1;
	    while (i > 0 && v[i] == 0) i--;
	    cout<<v[i];
	    for (i--; i >= 0; i--) {
	        cout<<setw(18)<<setfill('0')<<v[i];
	    }
	}
};

const int hv = 1e18;

const i128 inf = 1e38;
const int mxn = 305;
const int mod = 998244353;
int n,m;
bigint k;
int U[2*mxn],V[2*mxn],W[2*mxn]; //edges
i128 cyc[mxn][mxn];
i128 dist[mxn][mxn];
i128 d1[2*mxn*mxn][mxn];
bigint dp[2][mxn];
vector<pair<bigint,int>> update[mxn*mxn];

void solve() {
    cin>>n>>m;
    string KTT; cin>>KTT;
    k = bigint(KTT);
    for (int i=0; i<m; i++) {
        cin>>U[i]>>V[i]>>W[i];
        --U[i]; --V[i];
    }
    for (int cs=0; cs<n; cs++) {
        for (int i=0; i<=n; i++) {
            for (int j=0; j<n; j++) {
                dist[i][j] = inf;
            }
        }
        dist[0][cs] = 0;
        for (int i=1; i<=n; i++) {
            for (int ne=0; ne<m; ne++) {
                dist[i][V[ne]] = min(dist[i][V[ne]],dist[i-1][U[ne]]+W[ne]);
            }
            cyc[cs][i] = dist[i][cs];
        }
    }
		
    for (int i=0; i<2*n*n+5; i++) {
        for (int j=0; j<n; j++) {
            d1[i][j] = inf;
        }
    }
    d1[0][0] = 0;
    for (int i=1; i<2*n*n+5; i++) {
        for (int ne=0; ne<m; ne++) {
            d1[i][V[ne]] = min(d1[i][V[ne]],d1[i-1][U[ne]]+W[ne]);
        }
    }
    
    if (k.lesser(n*n*2)) {
        int kv = k.v[0];
        for (int i=0; i<n; i++) {
            cout<<(d1[kv][i]==inf?(i128)-1:d1[kv][i]%mod)<<" ";
        }
        cout<<"\n";
        return;
    }
    
    k.subtract(2*n*n-n);
	
	for (int i=0; i<n; i++) {
		i128 dd = d1[n*n][i];
		if (dd==inf) continue;
		for (int cl=1; cl<=n; cl++) {
			if (cyc[i][cl]==inf) continue;
			bigint amt; amt.copy(k);
			amt.divide(cl);
			int residue = k.remainder(cl);
			amt.multiply(cyc[i][cl]);
			amt.add(dd);
			update[residue+n*n-n].push_back({amt,i});
//			amt.print();
//			cout<<"\n";
		}
	}

	bigint temp;
	
	int cp=0,pp=1;
	
	for (int i=0; i<2; i++) {
		for (int j=0; j<n; j++) {
			dp[i][j].setinf();
		}
	}
	
	for (int cd = n*n+4; cd >= 0; cd--) {
		
		swap(cp,pp);
		for (int i=0; i<mxn; i++) {
			dp[cp][i].setinf();
		}
		for (auto [val,ind] : update[cd]) {
			dp[cp][ind].setmin(val);
		}
		for (int ne=0; ne<m; ne++) {
			if (dp[pp][U[ne]].isinf()) continue;
			temp.copy(dp[pp][U[ne]]);
			temp.add(W[ne]);
			dp[cp][V[ne]].setmin(temp);// = MIN(dp[cd][V[ne]],dp[cd+1][U[ne]]+BigInt(W[ne]));
		}
	}
	
	for (int i=0; i<n; i++) {
		if (dp[cp][i].isinf()) cout<<"-1 ";
		else cout<<dp[cp][i].remainder(mod)<<" ";
	}
	cout<<"\n";
}

/*
slow multiplcation ok
slow division ok
fast addition, comparison ... easy
*/

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	
	int s,t; cin>>s>>t;
	while (t--) {
		solve();
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 39ms
memory: 102460kb

input:

1
1
100 101 899539897889989959
74 35 910832669819965536
35 85 910832669819965536
85 88 910832669819965536
88 30 910832669819965536
30 58 910832669819965536
58 60 910832669819965536
60 34 910832669819965536
34 8 910832669819965536
8 67 910832669819965536
67 89 910832669819965536
89 32 910832669819965...

output:

395495792 395495781 395495783 395495839 395495793 395495789 395495754 395495832 395495845 395495755 395495823 395495773 395495753 395495800 395495782 395495763 395495847 395495761 395495794 395495791 395495786 395495821 395495798 395495765 395495772 395495770 395495757 395495819 395495843 395495828 ...

result:

ok 100 numbers

Test #2:

score: 10
Accepted
time: 77ms
memory: 102740kb

input:

1
1
100 200 998858598565699977
89 61 596014036562538539
89 84 921297646113897322
61 84 946923234442637386
61 35 641628261157284465
84 35 979893473772327497
84 78 700172488379560611
35 78 963617193748189613
35 54 951598888254521423
78 54 680825215292116806
78 21 737055858973038555
54 21 7491794406112...

output:

590375247 265938345 203065828 597548045 369717762 226160283 377877020 360218254 956162456 408060901 387231165 759578975 67601808 790211315 608425007 343195480 177353482 436533546 717630459 417099733 542227025 861764246 913806375 587268602 989846681 435016550 66609901 817090566 256847656 844441854 94...

result:

ok 100 numbers

Test #3:

score: 10
Accepted
time: 64ms
memory: 106904kb

input:

1
1
100 181 348568663892999968
25 19 990622898175774733
19 94 871060999389241529
94 24 969317630558501400
24 43 908457844888427461
43 52 816088481082287266
52 62 978618931332609685
62 99 761714433396732044
99 85 741344935503895668
85 64 964684335126604843
64 69 988098065125373655
69 31 7506975506815...

output:

916998469 916998469 916998469 76035207 62461893 916998469 389136594 916998469 916998469 173423529 164423356 822964468 626456020 916998469 744111524 916998469 398953850 916998469 342238577 916998469 255074799 784015663 916998469 740933556 587088671 811719512 916998469 916998469 916998469 916998469 14...

result:

ok 100 numbers

Test #4:

score: 10
Accepted
time: 67ms
memory: 102932kb

input:

1
1
100 189 295064635124730243
18 50 754672892083203214
50 88 962632394366987404
88 15 906700334097319336
15 26 967741400981618572
26 91 996214498763867892
91 35 882157548994344280
35 68 983621159612138407
68 51 563935036482744182
51 75 991205513962219551
75 72 974025375183814852
72 11 7979447663592...

output:

663199381 739882534 663199381 28600701 663199381 944601671 836329160 894091561 629507606 663199381 246830507 663199381 491987421 663199381 802123884 663199381 663199381 663199381 414785533 989396289 663199381 663199381 663199381 663199381 663199381 663199381 663199381 663199381 663199381 663199381 4...

result:

ok 100 numbers

Test #5:

score: 0
Wrong Answer
time: 65ms
memory: 24524kb

input:

1
254
40 74 997173688939799978
38 6 890721839505665075
6 10 992308491267087930
10 29 960202932780090595
29 20 952827125924298715
20 34 868314670055961466
34 31 756448635709788087
31 14 857625921909632516
14 18 917667459973696862
18 21 985939328882662624
21 1 734882468602343649
1 11 66102593854575036...

output:

177014577 177014577 177014577 885341552 472856470 177014577 363547548 177014577 499847464 653076748 177014577 177014577 177014577 177014577 487939796 177014577 213466543 586729345 244952763 177014577 177014577 177014577 177014577 890105934 177014577 177014577 890105934 177014577 177014577 798890006 ...

result:

wrong answer 109th numbers differ - expected: '242948151', found: '167386358'

Subtask #2:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 1055ms
memory: 870728kb

input:

2
1
300 598 8179377797889487867988994778539839593376697796496698959964978969
1 2 977880533270721156
2 1 977880533270721156
2 3 977880533270721156
3 2 977880533270721156
3 4 977880533270721156
4 3 977880533270721156
4 5 977880533270721156
5 4 977880533270721156
5 6 977880533270721156
6 5 977880533270...

output:

-1 797955760 -1 797945598 -1 797935436 -1 797925274 -1 797915112 -1 797904950 -1 797894788 -1 797884626 -1 797874464 -1 797864302 -1 797854140 -1 797843978 -1 797833816 -1 797823654 -1 797813492 -1 797803330 -1 797793168 -1 797783006 -1 797772844 -1 797762682 -1 797752520 -1 797742358 -1 797732196 -...

result:

wrong answer 2nd numbers differ - expected: '313446627', found: '797955760'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

0%