题面(本人翻译)

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.

一个三角形是 Heron 三角形仅当它的三边长是连续的正整数 t - 1, t, t + 1, 并且面积是正整数。现在,给你一个整数 N ,求大于等于 N 的最小的合法的 t (Heron 三角形的第二小的边)。

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).

一个正整数 T 表示数据组数,1 ≤ T ≤ 30000。接下来 T 行每行一个整数 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.

每个数据输出一行,即题意中的最小的 t ,如果没有满足要求的 Heron 三角形,输出 -1。

Sample Input

4
1
2
3
4

Sample Output

4
4
4
4

题解

我们可以用海伦公式表示面积

我们设 x = t/2,y = 2S/t,那么

这是pell方程的形式,所以先手算出最小的解 x=2,y=3,然后我们用pell方程的递推式:

我们会发现X增长得很快,到第52个就超过十的三十次方了,因此我们可以先打个表

X[1] = 4;
X[2] = 14;
X[3] = 52;
X[4] = 194;
X[5] = 724;
X[6] = 2702;
X[7] = 10084;
X[8] = 37634;
X[9] = 140452;
X[10] = 524174;
X[11] = 1956244;
X[12] = 7300802;
X[13] = 27246964;
X[14] = 101687054;
X[15] = 379501252;
X[16] = 1416317954;
X[17] = 5285770564;
X[18] = 19726764302;
X[19] = 73621286644;
X[20] = 274758382274;
X[21] = 1025412242452;
X[22] = 3826890587534;
X[23] = 14282150107684;
X[24] = 53301709843202;
X[25] = 198924689265124;
X[26] = 742397047217294;
X[27] = 2770663499604052;
X[28] = 10340256951198914;
X[29] = 38590364305191604;
X[30] = 144021200269567502;
X[31] = 537494436773078404;
X[32] = 2005956546822746114;
X[33] = 7486331750517906052;
X[34] = 27939370455248878094;
X[35] = 104271150070477606324;
X[36] = 389145229826661547202;
X[37] = 1452309769236168582484;
X[38] = 5420093847118012782734;
X[39] = 20228065619235882548452;
X[40] = 75492168629825517411074;
X[41] = 281740608900066187095844;
X[42] = 1051470266970439230972302;
X[43] = 3924140458981690736793364;
X[44] = 14645091568956323716201154;
X[45] = 54656225816843604128011252;
X[46] = 203979811698418092795843854;
X[47] = 761263020976828767055364164;
X[48] = 2841072272208896975425612802;
X[49] = 10603026067858759134647087044;
X[50] = 39571031999226139563162735374;
X[51] = 147681101929045799118003854452;
X[52] = 551153375716957056908852682434;
X[53] = 2056932400938782428517406875284; // 此处就超过 10^30 了

然后就二分判断就过了。

这题根本就不存在 -1。

CODE

zxy tql %%%%%%

#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#include<bitset>
#include<ctime>
#include<iostream>
#define MAXN 2005
#define LL long long
#define ULL unsigned LL
#define rg register
#define lowbit(x) (-(x) & (x))
#define ENDL putchar('\n')
#define DB double
//#define bs bitset<1005>
//#pragma GCC optimize(2)
//#pragma G++ optimize(3)
//#define int LL
using namespace std;
char char_read_before = 1;
inline int read() {
int f = 1,x = 0;char s = char_read_before;
while(s < '0' || s > '9') {if(s == '-') f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 - '0' + s;s = getchar();}
char_read_before = s; return x * f;
}
inline char readchar() {
char s = char_read_before;
while(s == 1 || s == ' ' || s == '\n') s = getchar();
char_read_before = 1; return s;
}
LL zxy = 100000000;
int n,m,i,j,s,o,k;
DB bg;inline DB Time() {return DB(clock() - bg) / CLOCKS_PER_SEC;} struct Num{
LL s[4];
Num(){s[0]=s[1]=s[2]=s[3]=0;}
Num(int b) {s[0] = b;s[1] = s[2] = s[3] = 0;}
Num operator = (int b) {
s[0] = b;
s[1] = s[2] = s[3] = 0;
return *this;
}
void tl() {
s[0] *= 10;
s[1] = s[1] * 10 + s[0] / zxy;
s[2] = s[2] * 10 + s[1] / zxy;
s[3] = s[3] * 10 + s[2] / zxy;
s[0] %= zxy;
s[1] %= zxy;
s[2] %= zxy;
}
};
inline Num operator *(Num a,Num b) {
Num c;
for(int i = 0;i < 4;i ++) {
LL m = 0;
for(int j = 0;i+j < 4;j ++) {
c.s[i+j] += a.s[i] *1ll* b.s[j] + m;
m = c.s[i+j] / zxy;
c.s[i+j] %= zxy;
}
}return c;
}
inline Num operator +(Num a,Num b) {
LL m = 0;
for(int i=0;i<4;i++) {
a.s[i] += b.s[i] + m;
m = a.s[i] / zxy;
a.s[i] %= zxy;
}return a;
}
inline bool operator < (Num a,Num b) {
if(a.s[3] != b.s[3]) return a.s[3] < b.s[3];
if(a.s[2] != b.s[2]) return a.s[2] < b.s[2];
if(a.s[1] != b.s[1]) return a.s[1] < b.s[1];
return a.s[0] < b.s[0];
}
inline bool operator >= (Num a,Num b) {return !(a < b);}
inline void print(Num a) {
int le = 0;
if(a.s[3]) le = 3;
else if(a.s[2]) le = 2;
else if(a.s[1]) le = 1;
printf("%d",a.s[le]);
while(le --) printf("%08d",a.s[le]);
return ;
}
inline Num readn() {
int f = 1;Num x(0);char s = char_read_before;
while(s < '0' || s > '9') {if(s == '-') f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x.tl();x = x + Num(s - '0');s = getchar();}
char_read_before = s; return x;
} struct mat{
int n,m;
Num s[3][3];
mat(){n=m=0;s[1][1]=s[1][2]=s[2][1]=s[2][2]=Num();}
}A,B;
inline mat operator * (mat a,mat b) {
mat c; c.n = a.n;c.m = b.m;
for(int i=1;i<=c.n;i++)
for(int k=1;k<=a.m;k++)
for(int j=1;j<=c.m;j++)
c.s[i][j] = c.s[i][j] + a.s[i][k] * b.s[k][j];
return c;
} Num as[100];
signed main() {
bg = clock();
A.n = 1;
A.m = B.n = B.m = 2;
A.s[1][1] = 2;
A.s[1][2] = 3;
B.s[1][1] = 2;
B.s[2][1] = 1;
B.s[1][2] = 3;
B.s[2][2] = 2;
for(int i = 0;i <= 52;i ++) {
as[i] = A.s[1][1] * Num(2);
A = A * B;
}
int T = read();
while(T --) {
Num nn = readn();
int l = 0,r = 52,mid;
while(l < r) {
mid = l + r >> 1;
if(as[mid] >= nn) r = mid;
else l = mid+1;
}
print(as[l]);
ENDL;
}
return 0;
}

HDU 6222 Heron and His Triangle (pell 方程)的更多相关文章

  1. Heron and His Triangle HDU - 6222(pell 大数)

    ---恢复内容开始--- Heron and His Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/2 ...

  2. HDU 2281 Square Number Pell方程

    http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...

  3. Pell方程及其一般形式

    一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...

  4. hdu 3304 Interesting Yang Yui Triangle

    hdu 3304 Interesting Yang Yui Triangle 题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 ...

  5. hdu3293(pell方程+快速幂)

    裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: ...

  6. POJ 1320 Street Numbers Pell方程

    http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b  要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...

  7. POJ 2427 Smith's Problem Pell方程

    题目链接 :  http://poj.org/problem?id=2427 PELL方程几个学习的网址: http://mathworld.wolfram.com/PellEquation.html ...

  8. Heron and His Triangle HDU - 6222

    题目链接:https://vjudge.net/problem/HDU-6222 思路:打表找规律. 然后因为数据范围较大可以考虑用字符串模拟,或者__int128要注意用一个快读快输模板. 1 #i ...

  9. Heron and His Triangle 2017 沈阳区域赛

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

随机推荐

  1. JS数组at函数(获取最后一个元素的方法)介绍

    本文介绍js中数组的at函数,属于比较简单的知识普及性文章,难度不大. 0x00 首先,我们可以思考如下一个问题,如果要获取一个数组的最后一个元素(这是很常用的操作),我们应该怎么做? 相信大部分人能 ...

  2. 透过Redis源码探究字符串的实现

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的Redis 5.0源码 概述 最近在通过 Redis 学 C 语言,不得不说, ...

  3. 12.web基础与HTTP协议

    web基础与HTTP协议 目录 web基础与HTTP协议 web基础 域名概述 HTML概述 HTML基本标签 HTML语法规则 HTML文件结构 头标签中常用标签 内容标签中常用标签 静态网页与动态 ...

  4. JS:函数

    Function:函数 1. 定义一个函数:function functionname(argument) { 代码块 return }: 调用此函数:fn() 2.函数是定义了一种方法,只有被调用才 ...

  5. SAP 复制Client

    原文链接:https://fenginfo.com/102.html 枫竹丹青 SCCL 复制客户端 进入了客户端复制主界面,首先选择参数文件(Selected Profile),虽然此条目为灰色的但 ...

  6. SAP Using Text Modules in Adobe Forms

    In this demo we will create an adobe form which displays text in two different languages (English or ...

  7. KVM虚拟机安装及桥接网络配置

    1.查看CPU是否支持intel或AMD的虚拟技术 cat /proc/cpuinfo | grep -E "vmx|svm" --color --vmx intel的CPU sv ...

  8. 毕设着急了吧?Python股票数据分析,制作动态柱状图

    写在前面的一些屁话: 雪球成立于 2010 年,是北京雪球信息科技有限公司旗下推出的投资者社区.雪球一直致力于为中国投资者提供跨市场(沪深.香港.美国),跨品种(股票.基金.债券等)的数据查询.资讯获 ...

  9. nginx配置的server_name无法访问

    问题: 我的nginx.conf配置文件中的server_name是这样子的,然后无法访问. 但是如果说server_name后面改成服务器的IP地址却是可以访问的. 解决方案: 在本机上(不是服务器 ...

  10. day11 - 多线程

    1内容 进程.线程介绍 Java中 线程的实现方式 Thread 类 Runnable 接口 Callable 接口 线程相关的方法 线程安全问题 - 同步技术 线程等待唤醒机制 进程(Process ...