HDU1977:Consecutive sum II

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;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值