HDU 6222 Heron and His Triangle (pell 方程)
题面(本人翻译)
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 方程)的更多相关文章
- Heron and His Triangle HDU - 6222(pell 大数)
---恢复内容开始--- Heron and His Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/2 ...
- HDU 2281 Square Number Pell方程
http://acm.hdu.edu.cn/showproblem.php?pid=2281 又是一道Pell方程 化简构造以后的Pell方程为 求出其前15个解,但这些解不一定满足等式,判断后只有5 ...
- Pell方程及其一般形式
一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...
- hdu 3304 Interesting Yang Yui Triangle
hdu 3304 Interesting Yang Yui Triangle 题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 ...
- hdu3293(pell方程+快速幂)
裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: ...
- POJ 1320 Street Numbers Pell方程
http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b 要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...
- POJ 2427 Smith's Problem Pell方程
题目链接 : http://poj.org/problem?id=2427 PELL方程几个学习的网址: http://mathworld.wolfram.com/PellEquation.html ...
- Heron and His Triangle HDU - 6222
题目链接:https://vjudge.net/problem/HDU-6222 思路:打表找规律. 然后因为数据范围较大可以考虑用字符串模拟,或者__int128要注意用一个快读快输模板. 1 #i ...
- Heron and His Triangle 2017 沈阳区域赛
A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...
随机推荐
- 记录bug的贴子
这个贴子用来记录一些,平时关注新闻,暴露出来的bug,引以为戒. 2019/01/21 - 拼多多出现大量100元无门槛券 关键词: 风险控制:羊毛党: https://www.zhihu.com/q ...
- pyenv安装及使用教程
pyenv安装及使用教程 pyenv 安装 git clone https://github.com/pyenv/pyenv.git ~/.pyenv # 编辑 bashrc vim ~/.bashr ...
- node环境下怎样优化引入多文件(实现自动化)
const mocks = [];function getJsonFiles(jsonPath) {function findJsonFile(path) {let files = fs.readdi ...
- Linux shell 2>&1的意思
在脚本里经常看到 ./xxx.sh > /dev/null 2>&1 ./xxx.sh > log.file 2>&1 在shell中输入输出都有对应的文件描述 ...
- sql-关键词的大小写与注释
是否区分大小写 和 注释 大小写 oracle 自带的sqlplus: mysql 客户端 : Navicat: 注释 oracle 自带的sqlplus: mysql 客户端 : 小节 oracle ...
- 《Ranked List Loss for Deep Metric Learning》CVPR 2019
Motivation: 深度度量学习的目标是学习一个嵌入空间来从数据点中捕捉语义信息.现有的成对或者三元组方法随着模型迭代过程会出现大量的平凡组导致收敛缓慢.针对这个问题,一些基于排序结构的损失取得了 ...
- @RequestBody,@RequestParam是否能随意改变入参字母大小写
在工作中遇到了一个问题,很多接口有的入参是companyName,有的入参是companyname,实际上,这两入参能任意适配所有接口,甚至随意改变大小写! 1. @RequestBody是完全按照驼 ...
- testNG框架,使用@BeforeClass标注的代码,执行失败不抛出异常,只提示test ignore的解决方法
郁闷了好久的一个问题,排错调试的时候是真滴麻烦... Google一圈,发现是testNG的Bug,升级testNG>=6.9.5,就能解决.
- 从零开始制作【立体键盘】,画UI免写CSS,【盲打练习】的交互逻辑只用了10来行表达式!
手把手教你从空白页面开始通过拖拉拽可视化的方式制作[立体键盘]的静态页面,不用手写一行CSS代码,全程只用10来行表达式就完成了[盲打练习]的交互逻辑. 整个过程在众触应用平台进行,快速直观. 最终U ...
- ICMP 介绍
简介 ICMP(Internet 控制报文协议,Internet Control Message Protocol , RFC 792).主要用于在IP主机与路由器之间传递控制消息,用于报告主机是否可 ...