zoukankan      html  css  js  c++  java
  • hdu 4038 Stone The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest

    在网友的提示之下,我终于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;
    }
  • 相关阅读:
    JS 实现日期信息增加年数,月数,天数
    ROW_NUMBER() OVER函数的基本用法,也可用于去除重复行
    Oracle存储过程返回游标实例详解
    PL/Sql 中创建、调试、调用存储过程
    HTTP 错误 404.13
    oracle查询多行数据合并成一行数据
    C# 实现list=list.OrderBy(q=>q.字段名).ToList(); 按多个字段排序
    [bcc32 Error] ws2def.h(231): E2238 Multiple declaration for 'sockaddr'
    [bcc32 Error] typeinfo.h(154): E2367 Can't inherit RTTI class from non-RTTI base 'exception'
    sql server 语法 MSDN
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2173768.html
Copyright © 2011-2022 走看看