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.
than or equal to n.
InputThe 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).
OutputFor 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
题解:大数.找规律.a[n]=4*a[n-1]-a[n-2](附上C++大数模板)
1 #include <bits/stdc++.h> 2 #define rep(i,a,n) for(int i=a;i<n;++i) 3 #define per(i,a,n) for(int i=n-1;i>=a;--i) 4 #define fi first 5 #define se second 6 using namespace std; 7 // base and base_digits must be consistent 8 constexpr int base = 1000000000; 9 constexpr int base_digits = 9; 10 struct bigint{ 11 vector<int> z; 12 int sign; 13 bigint() : sign(1) {} 14 bigint(long long v) { *this = v; } 15 bigint& operator=(long long v) 16 { 17 sign = v < 0 ? -1 : 1; 18 v*=sign; 19 z.clear(); 20 for(; v > 0; v = v / base) z.push_back((int)(v % base)); 21 return *this; 22 } 23 24 bigint(const string& s) { read(s); } 25 26 bigint& operator+=(const bigint& other) 27 { 28 if (sign == other.sign) 29 { 30 for (int i = 0, carry = 0; i < other.z.size() || carry; ++i) 31 { 32 if(i==z.size()) z.push_back(0); 33 z[i] += carry + (i < other.z.size() ? other.z[i] : 0); 34 carry = z[i] >= base; 35 if(carry) z[i] -= base; 36 } 37 } 38 else if (other != 0 /* prevent infinite loop */) 39 { 40 *this -= -other; 41 } 42 return *this; 43 } 44 45 friend bigint operator+(bigint a, const bigint& b) 46 { 47 return a += b; 48 } 49 50 bigint& operator-=(const bigint& other) 51 { 52 if (sign == other.sign) 53 { 54 if (sign == 1 && *this >= other || sign == -1 && *this <= other) 55 { 56 for (int i = 0, carry = 0; i < other.z.size() || carry; ++i) 57 { 58 z[i] -= carry + (i < other.z.size() ? other.z[i] : 0); 59 carry = z[i] < 0; 60 if(carry) z[i] += base; 61 } 62 trim(); 63 } 64 else 65 { 66 *this = other - *this; 67 this->sign = -this->sign; 68 } 69 } 70 else *this += -other; 71 return *this; 72 } 73 74 friend bigint operator - (bigint a,const bigint& b) 75 { 76 return a -= b; 77 } 78 79 bigint& operator*=(int v) 80 { 81 if(v<0) sign=-sign,v=-v; 82 for(int i=0,carry=0;i<z.size() || carry;++i) 83 { 84 if(i==z.size()) z.push_back(0); 85 long long cur = (long long)z[i] * v + carry; 86 carry = (int)(cur / base); 87 z[i] = (int)(cur % base); 88 } 89 trim(); 90 return *this; 91 } 92 93 bigint operator*(int v) const 94 { 95 return bigint(*this) *= v; 96 } 97 98 friend pair<bigint, bigint> divmod(const bigint& a1, const bigint& b1) 99 { 100 int norm = base / (b1.z.back() + 1); 101 bigint a = a1.abs() * norm; 102 bigint b = b1.abs() * norm; 103 bigint q, r; 104 q.z.resize(a.z.size()); 105 106 for (int i = (int)a.z.size() - 1; i >= 0; i--) 107 { 108 r*=base; r+=a.z[i]; 109 int s1 = b.z.size() < r.z.size() ? r.z[b.z.size()] : 0; 110 int s2 = b.z.size() - 1 < r.z.size() ? r.z[b.z.size() - 1] : 0; 111 int d = (int)(((long long)s1 * base + s2) / b.z.back()); 112 r -= b * d; 113 while(r < 0) r+=b,--d; 114 q.z[i] = d; 115 } 116 117 q.sign = a1.sign * b1.sign; 118 r.sign = a1.sign; 119 q.trim(); 120 r.trim(); 121 return {q, r / norm}; 122 } 123 124 friend bigint sqrt(const bigint& a1) 125 { 126 bigint a=a1; 127 while(a.z.empty()||a.z.size()%2==1) a.z.push_back(0); 128 129 int n = a.z.size(); 130 int firstDigit = (int)::sqrt((double)a.z[n - 1] * base + a.z[n - 2]); 131 int norm = base / (firstDigit + 1); 132 a *= norm; 133 a *= norm; 134 while(a.z.empty()||a.z.size()%2==1) a.z.push_back(0); 135 136 bigint r = (long long)a.z[n - 1] * base + a.z[n - 2]; 137 firstDigit = (int)::sqrt((double)a.z[n - 1] * base + a.z[n - 2]); 138 int q = firstDigit; 139 bigint res; 140 for (int j = n / 2 - 1; j >= 0; j--) 141 { 142 for(;;--q) 143 { 144 bigint r1=(r-(res*2*base+q)*q)*base*base+(j>0?(long long)a.z[2*j-1]*base+a.z[2*j-2]:0); 145 if(r1>=0) { r=r1; break; } 146 } 147 res*=base;res+=q; 148 if(j>0) 149 { 150 int d1 = res.z.size() + 2 < r.z.size() ? r.z[res.z.size() + 2] : 0; 151 int d2 = res.z.size() + 1 < r.z.size() ? r.z[res.z.size() + 1] : 0; 152 int d3 = res.z.size() < r.z.size() ? r.z[res.z.size()]:0; 153 q = (int)(((long long)d1*base*base+(long long)d2*base+d3)/(firstDigit*2)); 154 } 155 } 156 157 res.trim(); 158 return res / norm; 159 } 160 161 bigint operator/(const bigint& v) const 162 { 163 return divmod(*this, v).first; 164 } 165 166 bigint operator%(const bigint& v) const 167 { 168 return divmod(*this, v).second; 169 } 170 171 bigint& operator/=(int v) 172 { 173 if(v<0) sign=-sign,v=-v; 174 for (int i = (int)z.size() - 1, rem = 0; i >= 0; --i) 175 { 176 long long cur = z[i] + rem * (long long)base; 177 z[i] = (int)(cur / v); 178 rem = (int)(cur % v); 179 } 180 trim(); 181 return *this; 182 } 183 184 bigint operator/(int v) const 185 { 186 return bigint(*this) /= v; 187 } 188 189 int operator%(int v) const 190 { 191 if(v<0) v=-v; 192 int m=0; 193 for(int i=(int)z.size()-1;i>=0;--i) m=(int)((z[i]+m*(long long)base)%v); 194 return m * sign; 195 } 196 197 bigint& operator*=(const bigint& v) 198 { 199 *this = *this * v; 200 return *this; 201 } 202 203 bigint& operator/=(const bigint& v) 204 { 205 *this = *this / v; 206 return *this; 207 } 208 209 bool operator<(const bigint& v) const 210 { 211 if(sign!=v.sign) return sign < v.sign; 212 if(z.size()!=v.z.size()) return z.size()*sign<v.z.size()*v.sign; 213 for(int i = (int)z.size() - 1; i >= 0; i--) 214 if(z[i] != v.z[i]) return z[i] * sign < v.z[i] * sign; 215 return false; 216 } 217 218 bool operator>(const bigint& v) const { return v < *this; } 219 bool operator<=(const bigint& v) const { return !(v < *this); } 220 bool operator>=(const bigint& v) const { return !(*this < v); } 221 bool operator==(const bigint& v) const { return !(*this < v) && !(v < *this); } 222 bool operator!=(const bigint& v) const { return *this < v || v < *this; } 223 224 void trim() 225 { 226 while(!z.empty() && z.back() == 0) z.pop_back(); 227 if(z.empty()) sign = 1; 228 } 229 230 bool isZero() const { return z.empty(); } 231 232 friend bigint operator-(bigint v) 233 { 234 if(!v.z.empty()) v.sign = -v.sign; 235 return v; 236 } 237 238 bigint abs() const 239 { 240 return sign == 1 ? *this : -*this; 241 } 242 243 long long longValue() const 244 { 245 long long res = 0; 246 for(int i = (int)z.size() - 1; i >= 0; i--) res = res * base + z[i]; 247 return res * sign; 248 } 249 250 friend bigint gcd(const bigint& a, const bigint& b) 251 { 252 return b.isZero() ? a : gcd(b, a % b); 253 } 254 255 friend bigint lcm(const bigint& a, const bigint& b) 256 { 257 return a / gcd(a, b) * b; 258 } 259 260 void read(const string& s) 261 { 262 sign = 1; 263 z.clear(); 264 int pos = 0; 265 while(pos < s.size() && (s[pos] == '-' || s[pos] == '+')) 266 { 267 if(s[pos] == '-') sign = -sign; 268 ++pos; 269 } 270 for(int i=(int)s.size()-1;i>=pos;i-=base_digits) 271 { 272 int x=0; 273 for(int j=max(pos,i-base_digits+1);j<=i;j++) x=x*10+s[j]-'0'; 274 z.push_back(x); 275 } 276 trim(); 277 } 278 279 friend istream& operator>>(istream& stream, bigint& v) 280 { 281 string s; 282 stream >> s; 283 v.read(s); 284 return stream; 285 } 286 287 friend ostream& operator<<(ostream& stream, const bigint& v) 288 { 289 if(v.sign == -1) stream << '-'; 290 stream << (v.z.empty() ? 0 : v.z.back()); 291 for(int i = (int)v.z.size() - 2; i >= 0; --i) 292 stream << setw(base_digits) << setfill('0') << v.z[i]; 293 return stream; 294 } 295 296 static vector<int> convert_base(const vector<int>& a, int old_digits, int new_digits) 297 { 298 vector<long long> p(max(old_digits, new_digits) + 1); 299 p[0] = 1; 300 for(int i=1;i<p.size();i++) p[i]=p[i-1]*10; 301 vector<int> res; 302 long long cur = 0; 303 int cur_digits = 0; 304 for(int v : a) 305 { 306 cur += v * p[cur_digits]; 307 cur_digits += old_digits; 308 while (cur_digits >= new_digits) 309 { 310 res.push_back(int(cur % p[new_digits])); 311 cur /= p[new_digits]; 312 cur_digits -= new_digits; 313 } 314 } 315 res.push_back((int)cur); 316 while(!res.empty() && res.back()==0) 317 res.pop_back(); 318 return res; 319 } 320 321 typedef vector<long long> vll; 322 static vll karatsubaMultiply(const vll& a, const vll& b) 323 { 324 int n=a.size(); 325 vll res(n + n); 326 if(n <= 32) 327 { 328 for (int i = 0; i < n; i++) 329 for (int j = 0; j < n; j++) 330 res[i + j] += a[i] * b[j]; 331 return res; 332 } 333 334 int k = n >> 1; 335 vll a1(a.begin(), a.begin() + k); 336 vll a2(a.begin() + k, a.end()); 337 vll b1(b.begin(), b.begin() + k); 338 vll b2(b.begin() + k, b.end()); 339 vll a1b1 = karatsubaMultiply(a1, b1); 340 vll a2b2 = karatsubaMultiply(a2, b2); 341 for(int i=0;i<k;i++) a2[i]+=a1[i]; 342 for(int i=0;i<k;i++) b2[i]+=b1[i]; 343 344 vll r = karatsubaMultiply(a2, b2); 345 for(int i=0;i<a1b1.size();i++) r[i]-=a1b1[i]; 346 for(int i=0;i<a2b2.size();i++) r[i]-=a2b2[i]; 347 for(int i=0;i<r.size();i++) res[i+k]+=r[i]; 348 for(int i=0;i<a1b1.size();i++) res[i]+=a1b1[i]; 349 for(int i = 0;i<a2b2.size();i++) res[i+n]+=a2b2[i]; 350 return res; 351 } 352 353 bigint operator*(const bigint& v) const 354 { 355 vector<int> a6=convert_base(this->z,base_digits,6); 356 vector<int> b6=convert_base(v.z,base_digits,6); 357 vll a(a6.begin(),a6.end()); 358 vll b(b6.begin(),b6.end()); 359 while(a.size()<b.size()) a.push_back(0); 360 while(b.size()<a.size()) b.push_back(0); 361 while(a.size()&(a.size()-1)) a.push_back(0),b.push_back(0); 362 vll c=karatsubaMultiply(a, b); 363 bigint res; 364 res.sign = sign * v.sign; 365 for (int i = 0, carry = 0; i < c.size(); i++) 366 { 367 long long cur = c[i] + carry; 368 res.z.push_back((int)(cur % 1000000)); 369 carry = (int)(cur / 1000000); 370 } 371 res.z = convert_base(res.z, 6, base_digits); 372 res.trim(); 373 return res; 374 } 375 }; 376 377 bigint qpow(bigint a,bigint b){ 378 bigint ans=1; 379 while(b!=0){ 380 if(b%2){ 381 ans= ans*a; 382 } 383 b/=2; 384 a= a*a; 385 } 386 return ans; 387 388 } 389 390 391 struct Matrix 392 { 393 bigint a[2][2]; 394 Matrix() 395 { 396 rep(i,0,2){ 397 rep(j,0,2){ 398 a[i][j]=0; 399 } 400 } 401 } 402 Matrix operator * (const Matrix y) 403 { 404 Matrix ans; 405 for(int i = 0; i < 2; i++) 406 for(int j = 0; j < 2; j++) 407 for(int k = 0; k < 2; k++) 408 ans.a[i][j] = ans.a[i][j] + (a[i][k]*y.a[k][j]); 409 return ans; 410 } 411 Matrix operator = (const Matrix y) 412 { 413 for(int i=0;i<2;i++) 414 for(int j=0;j<2;j++) 415 a[i][j]=y.a[i][j]; 416 } 417 Matrix operator *= (const Matrix y) 418 { 419 Matrix ans; 420 for(int i=0;i<2;i++) 421 for(int j=0;j<2;j++) 422 for(int k=0;k<2;k++) 423 ans.a[i][j] = ans.a[i][j] + (a[i][k]*y.a[k][j]); 424 425 for(int i=0;i<2;i++) 426 for(int j=0;j<2;j++) 427 a[i][j]=ans.a[i][j]; 428 } 429 }; 430 431 Matrix qpow(bigint x) 432 { 433 Matrix ans; 434 ans.a[0][0]=ans.a[1][1]=1; //单位矩阵 435 Matrix mul; 436 mul.a[0][0]=4; 437 mul.a[0][1]=-1; 438 mul.a[1][0]=1; 439 mul.a[1][1]=0; 440 while(x!=0) 441 { 442 if(x%2!=0) 443 ans = ans*mul; 444 mul = mul* mul; 445 x/=2; 446 } 447 return ans; 448 } 449 bigint ans[1005]; 450 void solve(){ 451 452 453 ans[0]=(bigint)4; 454 ans[1]=(bigint)14; 455 ans[2]=(bigint)52; 456 rep(i,2,200){ 457 ans[i]=(bigint)4*ans[i-1]-ans[i-2]; 458 // cout<<ans[i]<<endl; 459 } 460 461 } 462 int main() 463 { 464 solve(); 465 int t;cin>>t; 466 while(t--){ 467 bigint n; 468 cin>>n; 469 rep(i,0,200){ 470 if(ans[i]>=n){ 471 cout<<ans[i]<<endl; 472 break; 473 } 474 } 475 476 477 } 478 }