hdu 4038 Stone The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
2011-09-11 20:54
573 查看
在网友的提示之下,我终于AC了!唉,好惨呀!比赛快结束时,才有了一丁点的想法!唉!
贴个代码,请指教!!
尽量多的3。对于负数,当有偶数个负数时,不必管它,若有奇数个负数,则将最大的负数尽量变为0,接着是使所有的0变为1,接着使所有的1变为2,接着使所有的2变为3。
最后将剩余的M变为3的X次方,此时M=M%3。若M=2,则要添加一个2,若是1,则要将正数中的最小数增加1,之后求积即可!
贴个代码,请指教!!
尽量多的3。对于负数,当有偶数个负数时,不必管它,若有奇数个负数,则将最大的负数尽量变为0,接着是使所有的0变为1,接着使所有的1变为2,接着使所有的2变为3。
最后将剩余的M变为3的X次方,此时M=M%3。若M=2,则要添加一个2,若是1,则要将正数中的最小数增加1,之后求积即可!
/* * hdu4038.c * * Created on: 2011-9-11 * Author: bjfuwangzhu */ #include<stdio.h> #include<string.h> #include<stdlib.h> #define LL long long #define nmax 1000010 #define nnum 1000000007 LL numa[nmax], numb[nmax]; int cmp(const void *a, const void *b) { return *(int *) a - *(int *) b; } int modular_exp(int a, LL b) { LL temp, res; res = 1; temp = a % nnum; while (b) { if (b & 1) { res = res * temp % nnum; } temp = temp * temp % nnum; b >>= 1; } return (int) res; } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif int T, i, j, N, alen, blen, x; LL M, res; while (scanf("%d", &T) != EOF) { for (i = 1; i <= T; i++) { scanf("%d %I64d", &N, &M); memset(numa, 0, sizeof(numa)); memset(numb, 0, sizeof(numb)); for (j = 0, alen = 0, blen = 0; j < N; j++) { scanf("%d", &x); if (x >= 0) { numa[alen++] = x; } else { numb[blen++] = x; } } if (blen & 1) { qsort(numb, blen, sizeof(numb[0]), cmp); while (M && (numb[blen - 1] != 1)) { M--, numb[blen - 1]++; } if (numb[blen - 1] > -1) { numa[alen++] = numb[blen - 1]; blen--; } } for (j = 0; j < alen; j++) { if (numa[j] == 0) { if (M) { numa[j]++; M--; } } } for (j = 0; j < alen; j++) { if (numa[j] == 1) { if (M) { numa[j]++; M--; } } } for (j = 0; j < alen; j++) { if (numa[j] == 2) { if (M) { numa[j]++; M--; } } } if (M >= 3) { numa[alen++] = modular_exp(3, M / 3); M %= 3; } qsort(numa, alen, sizeof(numa[0]), cmp); if (M == 2) { numa[alen++] = 2; } if (M == 1) { numa[0]++; } for (j = 0, res = 1; j < blen; j++) { res = res * numb[j] % nnum; } for (j = 0; j < alen; j++) { res = res * numa[j] % nnum; } printf("Case %d: %I64d\n", i, res); } } return 0; }
相关文章推荐
- HDU 4038 Stone The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
- HDU 4033 4036 4039 The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
- The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest题解
- HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
- HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
- 【转】The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest 出题报告
- 4045 Machine scheduling(The 36th ACM/ICPC Asia Regional Beijing Site —— Online Contest)
- The 36th ACM/ICPC Asia Regional Beijing Site Online Contest - G Panda
- The 37th ACM/ICPC Asia Regional ChengDu Site Online Contest - B Control
- The 36th ACM/ICPC Asia Regional Beijing Site Online Contest - G Panda
- HDU 4034 Graph The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest
- The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
- The 37th ACM/ICPC Asia Regional ChengDu Site Online Contest - F Groups
- The 36th ACM/ICPC Asia Regional Beijing Site Online Contest - B_Eliminate Witches!
- HDU4022-map+multiset--The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest
- 【栈+模拟】The 36th ACM/ICPC Asia Regional Beijing Site Online Contest - B Eliminate Witches!
- 4007 Dave(The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest)
- 【树状数组】The 36th ACM/ICPC Asia Regional Beijing Site Online Contest - G Panda