您的位置:首页 > 其它

HDU1977:Consecutive sum II

2013-04-17 20:05 351 查看
Problem Description
Consecutive sum come again. Are you ready? Go ~~

1 = 0 + 1

2+3+4 = 1 + 8

5+6+7+8+9 = 8 + 27



You can see the consecutive sum can be representing like that. The nth line will have 2*n+1 consecutive numbers on the left, the first number on the right equal with the second number in last line, and the sum of left numbers equal with two number’s sum on
the right.

Your task is that tell me the right numbers in the nth line.



Input
The first integer is T, and T lines will follow.

Each line will contain an integer N (0 <= N <= 2100000).



Output
For each case, output the right numbers in the Nth line.

All answer in the range of signed 64-bits integer.



Sample Input
3
0
1
2




Sample Output
0 1
1 8
8 27





这道题我只能说数据太水了

先说说题意吧

0 : 1 = 0 ^3+ 1^3

1: 2 + 3 + 4 = 1^3 + 2^3

2: 5 + 6 + 7 + 8 + 9 = 2^3 + 3^3

3: 10 + 11 + 12 + 13 + 14 + 15 = 3^3 + 4^3

所以得 n = n^3 + (n+1)^3

问题是n最大的2100000,理论就算是__int64也装不下

但是用__int64也过了,真是数据太水了= =

先贴大数的:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void mult(char a[],char b[],char s[])
{
    int i,j,k = 0,alen,blen,sum = 0,res[65][65]= {0},flag = 0;
    char result[65];
    alen = strlen(a);
    blen = strlen(b);
    for(i = 0; i<alen; i++)
    {
        for(j = 0; j<blen; j++)
            res[i][j] = (a[i]-'0')*(b[j]-'0');
    }
    for(i = alen-1; i>=0; i--)
    {
        for(j = blen-1; j>=0; j--)
        {
            sum = sum+res[i+blen-j-1][j];
        }
        result[k] = sum%10;
        k++;
        sum = sum/10;
    }
    for(i = blen-2; i>=0; i--)
    {
        for(j = 0; j<=i; j++)
        {
            sum = sum+res[i-j][j];
        }
        result[k] = sum%10;
        k++;
        sum = sum/10;
    }
    if(sum)
    {
        result[k] = sum;
        k++;
    }
    for(i = 0; i<k; i++)
        result[i]+='0';
    for(i = k-1; i>=0; i--)
        s[i] = result[k-1-i];
    s[k] = '\0';
    while(1)
    {
        if(strlen(s)!=strlen(a) && s[0] == '0')
            strcpy(s,s+1);
        else
            break;
    }
}

void add(char a[],char b[],char back[])
{
    int i,j,k,up,x,y,z,l;
    char *c;
    if(strlen(a) > strlen(b))
        l = strlen(a)+2;
    else
        l = strlen(b)+2;
    c = (char*)malloc(l*sizeof(char));
    i = strlen(a)-1;
    j = strlen(b)-1;
    k = 0;
    up = 0;
    while(j>=0 || i>=0)
    {
        if(i<0) x = '0';
        else
            x = a[i];
        if(j<0) y = '0';
        else
            y = b[j];
        z = x-'0'+y-'0';
        if(up)
            z++;
        if(z>9)
        {
            up = 1;
            z%=10;
        }
        else
            up = 0;
        c[k++] = z+'0';
        i--;
        j--;
    }
    if(up)
        c[k++] = '1';
    i = 0;
    c[k] = '\0';
    for(k-=1; k>=0; k--)
        back[i++] = c[k];
    back[i] = '\0';
}

int main()
{
    int k;
    char a[100],b[100],t[100];
    scanf("%d%*c",&k);
    while(k--)
    {
        scanf("%s",a);
        add(a,"1",b);
        strcpy(t,a);
        mult(t,t,a);
        mult(a,t,a);
        strcpy(t,b);
        mult(t,t,b);
        mult(b,t,b);
        printf("%s %s\n",a,b);
        memset(a,'\0',sizeof(a));
        memset(b,'\0',sizeof(b));
    }
    return 0;
}


然后是水:

#include <stdio.h>

int main()
{
    int k;
    scanf("%d",&k);
    while(k--)
    {
        __int64 n,m;
        scanf("%I64d",&n);
        m = n+1;
        n = n*n*n;
        m = m*m*m;
        printf("%I64d %I64d\n",n,m);
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: