链接 http://acm.hdu.edu.cn/showproblem.php?pid=6222
Problem Description
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
Source
2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题意
给你一个n,找到最小的t满足,t>=n,且t-1,t,t+1改成的三角形面积是整数
思路
打表,暴力枚举每个t,因为c++精度问题,到1e7之后很多都不是的
打出是这样的
“4”,
“14”,
“52”,
“194”,
“724”,
“2702”,
“10084”,
“37634”,
“140452”,
我直接oeis查出规律,其实不难找出规律:a[n]=4*a[n-1]-a[n-2]
不难发现跑到70左右就超过题目给定范围,然后我们可以用__int128或者string来做。
代码
#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
inline void input(int& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 1e6 + 5;
int Case;
string n;
string s[70]={
"4",
"14",
"52",
"194",
"724",
"2702",
"10084",
"37634",
"140452",
"524174",
"1956244",
"7300802",
"27246964",
"101687054",
"379501252",
"1416317954",
"5285770564",
"19726764302",
"73621286644",
"274758382274",
"1025412242452",
"3826890587534",
"14282150107684",
"53301709843202",
"198924689265124",
"742397047217294",
"2770663499604052",
"10340256951198914",
"38590364305191604",
"144021200269567502",
"537494436773078404",
"2005956546822746114",
"7486331750517906052",
"27939370455248878094",
"104271150070477606324",
"389145229826661547202",
"1452309769236168582484",
"5420093847118012782734",
"20228065619235882548452",
"75492168629825517411074",
"281740608900066187095844",
"1051470266970439230972302",
"3924140458981690736793364",
"14645091568956323716201154",
"54656225816843604128011252",
"203979811698418092795843854",
"761263020976828767055364164",
"2841072272208896975425612802",
"10603026067858759134647087044",
"39571031999226139563162735374",
"147681101929045799118003854452",
"551153375716957056908852682434",
"2056932400938782428517406875284",
"7676576228038172657160774818702",
"28649372511213908200125692399524",
"106920913816817460143341994779394",
"399034282756055932373242286718052",
"1489216217207406269349627152092814",
"5557830586073569145025266321653204",
"20742106127086870310751438134520002",
"77410593922273912097980486216426804",
"288900269562008778081170506731187214",
"1078190484325761200226701540708322052",
"4023861667741036022825635656102100994",
"15017256186638382891075841083700081924",
"56045163078812495541477728678698226702",
};
int main(){
Case=1;
input(Case);
while(Case--){
cin>>n;
for(int i=0;;i++){
if(s[i].size()>n.size()){
cout<<s[i]<<endl;
break;
}
else if(s[i].size()==n.size()){
if(s[i]>=n){
cout<<s[i]<<endl;
break;
}
}
}
}
return 0;
}