HDU 6222 Heron and His Triangle ( 2017 ICPC沈阳, 线性递推 + 大数)

Heron and His Triangle

A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integers t−1, t, t+ 1 and thatits area is an integer. Now, for given n you need to find a Heron’s triangle associated with the smallest t bigger 
than or equal to n.

Input

The input contains multiple test cases. The first line of a multiple input is an integer T (1 ≤ T ≤ 30000) followedby T lines. Each line contains an integer N (1 ≤ N ≤ 10^30). 

Output

For each test case, output the smallest t in a line. If the Heron’s triangle required does not exist, output -1.

Sample Input

4
1
2
3
4

Sample Output

4
4
4
4

题意:给你一个 n , 要求你找一个最小 t ,满足 t >= n 且 t - 1, t, t + 1 为边长所构成的三角形的面积为整数。

思路:先直接打表出前面几项 4 14 52 194 724 2702,然后可暴力求线性递推式系数 推出一个 线性递推式 a[n] = 4 * a[n-1] - a[n - 2],接着打表就可以了。   不会java大数,我也很难过。QAQ

AC代码:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)

using namespace std;

char ans[100][100];
char tmp[100];
char tmp1[100];
char four[10] = {'4', '\0'};

void bigjian(char *a, char *b) {
    int i = strlen(a) - 1, j = strlen(b) - 1;
    int x = 0, c[205] = {0}, flag = 1, d[205] = {0}, f[205] = {0};
    for(int l = 0; l <= i; l++)
        c[l] = a[l] - '0';
    for(int l = 0; l <= j; l++)
        d[l] = b[l] - '0';
    while(j != -1) {
        if(c[i] < d[j]) {
            c[i] += 10;
            c[i - 1] -= 1;
        }
        f[x++] = c[i] - d[j];
        i--;
        j--;
    }
    while(i >= 0) {
        if(c[i] < 0) {
            c[i] += 10;
            c[i - 1] -= 1;
        }
        f[x++] = c[i];
        i--;
    }
    int p = 0;
    if(f[x - 1] == 0)
        flag = 0;
    for(int y = x - 1; y >= 0; y--) {
        if(f[y] != 0)
            flag = 1;
        if(flag == 1)
            tmp[p ++] = f[y] + '0';
    }
    if(flag == 0)
        tmp[p ++] = '0';
    tmp[p] = '\0';
}

void bigx(char *a, char *b) {
    int x, k, lena, lenb, lenb1, c[150] = {0};
    lena = strlen(a) - 1;
    lenb = strlen(b);
    lenb1 = lenb;
    while(lenb--) {
        x = lenb1 - lenb - 1;
        for(int i = lena; i >= 0; i--) {
            k = (a[i] - '0') * (b[lenb] - '0');
            c[x] += k;
            if(c[x] >= 10) {
                c[x + 1] += c[x] / 10;
                c[x] %= 10;
            }
            x++;
        }
    }
    int p = 0;
    if(c[x] != 0)
        tmp1[p ++] = c[x] + '0';
    for(int i = x - 1; i >= 0; i--)
        tmp1[p ++] = c[i] + '0';
    tmp1[p] = '\0';
}

int main() {
    ans[0][0] = '2';
    ans[1][0] = '4';
    int i;
    for(i = 2; i <= 101; i ++) {
        memset(tmp, 0, sizeof(tmp));
        memset(tmp1, 0, sizeof(tmp1));
        bigx(ans[i - 1], four);   /// 存入 tmp1
        bigjian(tmp1, ans[i - 2]); /// 存入 tmp
        if(strlen(tmp) > 31) break;
        strcpy(ans[i], tmp);
    }
    int t; scanf("%d",&t);
    while(t --){
        char n[50]; scanf("%s",n);
        for(int j = 1;j <= i + 1;j ++){
            if(strlen(n) < strlen(ans[j])){
                printf("%s\n",ans[j]);
                break;
            }
            else {
                if(strlen(n) == strlen(ans[j])){
                    if(strcmp(n,ans[j]) <= 0){
                        printf("%s\n",ans[j]);
                        break;
                    }
                }
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值