P1965 夜夜的数据加强 题解
生活随笔
收集整理的這篇文章主要介紹了
P1965 夜夜的数据加强 题解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這是VIJIOS上的一道題,原題的鏈接https://vijos.org/p/1965
先上代碼,用C++寫的
# include<iostream> using namespace std;int fa[100001]; int savemod[400][400][400]; int ii[100000]; // a^(i-1) 從1開始使用 int jj[100000]; // 1+a+a^2+a^(i-2) 從1開始使用 bool ans[400][400][400];int len; int amod[400][400]; // amod[i][j] = i % j;int main() {int x, a, b;int i, j, k;int n, c;cin >> n >> c;for (i = 1; i <= n-1; i++){cin >> fa[i];if (fa[i] >= i || fa[i] < 0 || fa[i] >= c){cout << "Unsuccessful Hack Attempt" << endl;return 0;}}if (n == 1){for (x = 0; x < c; x++)for (a = 0; a < c; a++)for (b = 0; b < c; b++)cout << x << " " << a << " " << b << endl;return 0;}int tmp;for (i = 0; i < c; i++)for (j = i; j < c; j++){tmp = (i*j) % c;for (k = 0; k < c - tmp; k++) {savemod[i][j][k] = tmp + k;savemod[j][i][k] = tmp + k;}for (k = 0; k < tmp; k++){savemod[i][j][c-tmp+k] = k;savemod[j][i][c-tmp+k] = k;}}for (i = 1; i < 400; i++)for (j = 2; j < 400; j++) amod[i][j] = i % j;int s;bool flag = false;if (n - 1 > c){for (a = 0; a < c; a++){for (b = 0; b < c; b++){for (i = n-2; i >= c; i--){if (savemod[fa[i]][a][b] != fa[i+1]) break;}if (i < c) for (x = 0; x < c; x++){for (s = savemod[x][a][b],j = 2; j <= c && amod[s][j] == fa[j]; s=savemod[s][a][b], j++);if (j > c){ans[x][a][b] = true;flag = true;}}}}} else{for (a = 0; a < c; a++){ii[1] = 1;for (i = 2; i < n; i++){ii[i] = savemod[ii[i-1]][a][0];}jj[1] = 0;for (i = 2; i < n; i++){jj[i] = savemod[jj[i-1]][a][1];}for (b = 0; b < c; b++)for (x = 0; x < c; x++)if (amod[savemod[x][ii[n-1]][savemod[b][jj[n-1]][0]]][n-1] == fa[n-1] && amod[savemod[x][ii[2]][savemod[b][jj[2]][0]]][2] == fa[2]){for (j = n-2; j>2 && amod[savemod[x][ii[j]][savemod[b][jj[j]][0]]][j] == fa[j]; --j);if (j == 2){flag = true;ans[x][a][b] = true;}}}}if (flag == false){cout << "Unsuccessful Hack Attempt" << endl;} else{for (i = 0; i < c; i++)for (j = 0; j < c; j++)for (k = 0; k < c; k++) if (ans[i][j][k]) cout << i << " " << j << " " << k << endl;}return 0; }這里參考了該題doc寫的題解,題解的地址為https://vijos.org/p/1965/solution。如果單純用doc的暴力方法是不能通過的(能通過第5個(gè)測(cè)試點(diǎn),但是后面的測(cè)試點(diǎn)不能通過),我另外加了一個(gè)優(yōu)化。
考慮到N比C大很多的情況,這個(gè)時(shí)候計(jì)算fa[i]時(shí)取余運(yùn)算(%i),就不起作用了,可以簡(jiǎn)化為
fa[i+1] = (fa[i] * a + b) % c(這里i >= c)
在這里只和a和b有關(guān),與x無關(guān),因此可以先枚舉a和b,然后檢驗(yàn)a和b是否可行,如果可行就進(jìn)行,接下來枚舉x再校驗(yàn)數(shù)列i<=c的情況(此時(shí)后面不需要校驗(yàn)了)。
另外這個(gè)程序要仔細(xì)編寫,做盡可能的預(yù)處理,下面的測(cè)試結(jié)果可以看到在第5個(gè)測(cè)試點(diǎn)是很驚險(xiǎn)地通過了。
測(cè)試數(shù)據(jù) #0: Accepted, time = 15 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #1: Accepted, time = 0 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #2: Accepted, time = 0 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #3: Accepted, time = 0 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #4: Accepted, time = 937 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #5: Accepted, time = 0 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #6: Accepted, time = 0 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #7: Accepted, time = 0 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #8: Accepted, time = 15 ms, mem = 315188 KiB, score = 5測(cè)試數(shù)據(jù) #9: Accepted, time = 0 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #10: Accepted, time = 78 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #11: Accepted, time = 93 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #12: Accepted, time = 93 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #13: Accepted, time = 78 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #14: Accepted, time = 93 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #15: Accepted, time = 390 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #16: Accepted, time = 296 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #17: Accepted, time = 375 ms, mem = 315180 KiB, score = 5測(cè)試數(shù)據(jù) #18: Accepted, time = 437 ms, mem = 315184 KiB, score = 5測(cè)試數(shù)據(jù) #19: Accepted, time = 359 ms, mem = 315184 KiB, score = 5Accepted, time = 3259 ms, mem = 315188 KiB, score = 100總結(jié)
以上是生活随笔為你收集整理的P1965 夜夜的数据加强 题解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vba中的查找匹配函数
- 下一篇: Java的Socket通信(多Clien