您的位置:首页 > 其它

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,之后求积即可!

/*
* 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐