hdu6222——佩尔方程&&大数__int128
题意
给定一个整数 $N$($1 \leq N \leq 10^{30}$),求最小的整数 $t$,要求 $t \geq N$,使得边长为 $t-1, t, t+1$ 的三角形面积为整数。
分析
根据海伦公式:$S = \sqrt{p(p-a)(p-b)(p-c)}$,$p = \frac{a+b+c}{2}$.
代入,令 $\frac{t}{2}=x$,化简得 $S^2 = 3x^2(x^2-1)$.
题目要求 $S$ 为整数,则 $(x^2-1)$ 一定是3乘以一个平方数,
即 $x^2-1=3y^2$,即 $x^2-3y^2=1$.
易知最小解为(2, 1),用递推式求出其他解即可。
由于题目 $N$ 的范围较大,到 1e30,可以使用 int128(1e38),本地测试1e38内,只有67个解。
#include<bits/stdc++.h>
using namespace std; const int maxn = +;
__int128 xx[maxn], yy[maxn]; void read(__int128 &x) {
x = ;
char ch;
int flag = ;
while (ch = getchar()) {
if (ch == '-') flag = -;
if (ch >= '' && ch <= '') break;
}
x = ch-'';
while ((ch = getchar()) >= '' && ch <= '') {
x = x* + ch-'';
}
x *= flag;
} void out(__int128 x) {
if (x < ) {
x = -x;
putchar('-');
}
if (x >= ) out(x / );
putchar(x % +'');
} void init()
{
xx[] = , yy[] = ;
for(int i = ;i <= ;i++)
{
xx[i] = xx[i-]* + yy[i-]*;
yy[i] = xx[i-] + yy[i-]*;
}
} int main()
{
init();
int T;
scanf("%d", &T);
while(T--)
{
__int128 n;
read(n);
for(int i = ;i <= ;i++)
{
if(xx[i]* >= n)
{
out(xx[i]*);
printf("\n");
break;
}
}
}
}
顺便记个int128的模板,
hdu6222——佩尔方程&&大数__int128的更多相关文章
- POJ1320 Street Numbers【佩尔方程】
主题链接: http://poj.org/problem?id=1320 题目大意: 求解两个不相等的正整数N.M(N<M),使得 1 + 2 + - + N = (N+1) + - + M.输 ...
- POJ 1320 Street Numbers 【佩尔方程】
任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- HDU 3292 【佩尔方程求解 && 矩阵快速幂】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...
- POJ 1320 Street Numbers 解佩尔方程
传送门 Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2529 Accepted: 140 ...
- 2010辽宁省赛G(佩尔方程)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- C语言之基本算法26—佩尔方程求解
//穷举法! /* ====================================================== 题目:求佩尔方程x*x-73*y*y=1的解. =========== ...
- [NBUT 1224 Happiness Hotel 佩尔方程最小正整数解]连分数法解Pell方程
题意:求方程x2-Dy2=1的最小正整数解 思路:用连分数法解佩尔方程,关键是找出√d的连分数表示的循环节.具体过程参见:http://m.blog.csdn.net/blog/wh2124335/8 ...
- POJ 1320 Street Numbers(佩尔方程)
Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3078 Accepted: 1725 De ...
- Problem G: 深入浅出学算法008-求佩尔方程的解
Description 求关于x y的二次不定方程的解 x2-ny2=1 Input 多组输入数据,先输入组数T 然后输入正整数n(n<=100) Output 对于每组数据输出一行,求y< ...
随机推荐
- windows环境中hbase源码编译遇到的问题
转载请注明出处 问题一 [ERROR] Failed to execute goal org.codehaus.mojo:findbugs-maven-plugin:3.0.0:findbugs (d ...
- sql server取日期各个组成部分的datename()函数
SQL Server中的日期类型datetime的默认格式是yyyy-mm-dd hh:mi:ss:mmm,很多时候我们可能会需要获取日期中的某个组成部分,因此SQL Server提供了一个daten ...
- keyword:react native bridge
js engine native react native bridge https://cn.bing.com/images/search?view=detailV2&id=AAF39636 ...
- 《 .NET并发编程实战》实战习题集 - 2 - 替换算法
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- Console程序下监控EFCore生成的SQL语句!
最近这两天在使用控制台程序学习EFCore,突然想看看生成的SQL语句,所以在网上找到一位大神的分享的方法! 准备工作: 1). MySqlEFCore NuGet: Pomelo.EntityF ...
- 在 VSCode 调试过程中,使用 Watcher,免手动重新编译
1.安装Microsoft.DotNet.Watcher.Tools包 dotnet add package Microsoft.DotNet.Watcher.Tools --version 2.0. ...
- ASP.NET MVC自定义Module记录管道事件执行顺序
1. 在Visual Studio 新建项目,模板为空,下面结构选择MVC. 2. 在项目中新建一个类MyModule,实现IHttpModule接口 namespace SimpleApp.Infr ...
- 程序游戏推荐(C语言贪吃蛇,python天天酷跑(需要安装pygame),js是狠人就坚持30s)
下面是下载位置,我把他们上传到我的文件下了. C语言贪吃蛇:https://files.cnblogs.com/files/ITXiaoAng/%E8%B4%AA%E5%90%83%E8%9B%87. ...
- Linux操作:使用grep排除搜索的目录
使用grep时,当一个目录下有一个包含很多文件的目录,但也不想搜索它,怎么办? 使用 --exclude-dir 选项. 单个目录示例: grep -rni 'http' --exclude-dir= ...
- linux命令之——grep详解
grep命令用于查找文件里符合条件的字符串:也可以用于查找内容包含指定的范本样式的文件.它能使用正则表达式搜索,用于在文件中搜索指定的字符串模式,列出含有匹配模式子符串的文件名,并输出含有该字符串的文 ...