[HDU6222]Heron and His Triangle

2024-03-16 23:32
文章标签 triangle heron hdu6222

本文主要是介绍[HDU6222]Heron and His Triangle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Heron and His Triangle

题解

很水的一道找规律题呀

由T**M*E***的要求,还是写一下Pell方程的做法吧

由海伦公式可知,S= \sqrt{p(p-a)(p-b)(p-c)}

\because a=t-1,b= t,c=t+ 1\therefore p=\frac{a+b+c}{2}= \frac{3t}{2}

\therefore S= \sqrt{ \frac{3}{2} t \cdot (\frac{1}{2} t+ 1) \cdot \frac{1}{2} t \cdot (\frac{1}{2} t-1) }

x=\frac{t}{2},则S=\sqrt{3x^2(x^2-1)}

\therefore 3x^2(x^2-1)= S^2

由于S\in Z^{+},所以x^2-1肯定是平方数。

所以x^2-3y^2= 1

所以我们可以得到递推公式,即每个区间的取值公式:

a_{i}=4a_{i-1}-a_{i-2},找到比n大的最小的一个a_{i},输出即可。

源码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<stack>
using namespace std;
typedef __int128 _LL;
typedef long long LL;
typedef pair<int,int> pii;
template<typename _T>
void read(_T &x){_T f=1;x=0;char s=getchar();while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}x*=f;
}
template<typename _T>
void write(_T x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+'0');
}
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
struct bigInt{int a[505],len;bigInt(){memset(a,0,sizeof(a));len=0;}bigInt(int num){*this=bigInt();while(num) a[++len]=num%10,num/=10;}int& operator [] (int index){return a[index];}void getIn(){char s[505];scanf("%s",s+1);len=strlen(s+1);for(int i=1;i<=len;i++)a[len-i+1]=s[i]-'0';}void rev(){for(int i=1;i*2<=len;i++)swap(a[i],a[len-i+1]);}bigInt operator / (const int num){bigInt res=*this;int x=0;for(int i=len;i>0;i--)res[i]=(x*10+a[i])/num,x=(x*10+a[i])%num;res.len=len;while(res.len>1&&!res[res.len]) res.len--;return res;}int operator % (const int num){int x=0;for(int i=len;i>0;i--)x=(a[i]+x*10)%num;return x;}bool zero()const{return len==1&&!a[1];}void print()const{for(int i=len;i>0;i--)write(a[i]);puts("");}bigInt operator + (bigInt other){bigInt res=bigInt(0);int x=0;res.len=1;for(int i=1;i<=len||i<=other.len;i++,res.len++){res[i]=(a[i]+other[i]+x)%10;x=(a[i]+other[i]+x)/10;}res[res.len]=x;while(res.len>1&&!res[res.len]) res.len--;return res;}bigInt operator * (bigInt other){bigInt res=bigInt(0);for(int i=1;i<=len;i++){int x=0;for(int j=1;j<=other.len;j++){x=res[i+j-1]+a[i]*other[j]+x;res[i+j-1]=x%10;x/=10;}res[i+other.len]=x;}res.len=other.len+len;while(res.len>1&&!res[res.len])res.len--;return res;}bigInt operator * (const int num){return *this*bigInt(num);}bigInt operator - (bigInt other){bigInt res=*this;for(int i=1;i<=len;i++){if(res[i]<other[i])res[i+1]--,res[i]+=10;res[i]-=other[i];}while(res.len>1&&!res[res.len]) res.len--;return res;}bool operator < (bigInt &other)const{if(len<other.len)return true;if(len>other.len)return false;for(int i=len;i;i--){if(a[i]<other[i])return true;if(a[i]>other[i])return false;}return false;}bool operator > (bigInt &other)const{if(len>other.len)return true;if(len<other.len)return false;for(int i=len;i;i--){if(a[i]>other[i])return true;if(a[i]<other[i])return false;}return false;}bool operator == (bigInt &other)const{if(len<other.len)return false;if(len>other.len)return false;for(int i=len;i;i--){if(a[i]<other[i])return false;if(a[i]>other[i])return false;}return true;}bool judge(){return len>=32;}
}num[500],n,m1,m2,m3,m4;//以前做玛琪露数时的模板,改改就拿来用了
int compare(bigInt x,bigInt y){if(x==y)return 2;if(x>y)return 1;if(x<y)return 0;
}
int t;
signed main(){num[0]=bigInt(4);num[1]=bigInt(14);for(int i=2;i;i++){num[i]=num[i-1]*4-num[i-2];if(num[i].judge())break;}read(t);m1=bigInt(1);m2=bigInt(2);m3=bigInt(3);m4=bigInt(4);while(t--){n.getIn();int fg=0,cnt;if(n==m1||n==m2||n==m3||n==m4){puts("4");continue;}for(int i=0;;i++){int x=compare(n,num[i]);if(fg==1&&x!=1){cnt=i;break;}if(x==1)fg=1;}num[cnt].print();}return 0;
}

谢谢!!!

这篇关于[HDU6222]Heron and His Triangle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/817089

相关文章

leetCode#119. Pascal's Triangle II

Description Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Code

CodeForces 407A Triangle

题意: 一个直角三角形所有点都在二维平面整点上  其中两条边长度分别为a和b  且没有任何一条边与坐标轴平行  问  这样的三角形存不存在  如果存在输出一组坐标 思路: 可以设解存在  然后先固定(0,0)这个点  这样就可以求出所有满足边长是a和b的(x,y)坐标分别放在两个数组里 注意只枚举第一、二象限即可  要不还要防止三点共线 枚举a和b的所有解  如果这确定的三个点满足

National Contest for Private Universities (NCPU), 2019 E. Generalized Pascal's Triangle

编辑代码 2000ms 262144K Generalized Pascal's Triangle Pascal's triangle is a triangular array in which each number can be calculated by the sum of the two numbers directly above that number as shown i

UVA 11401 Triangle Counting

中文详解请访问我的博客:http://xiaoshig.sinaapp.com/?p=128 You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two

[HDU 4324] Triangle LOVE (拓扑排序,DFS)

HDU - 4324 题意是,一张有 N个点的图,保证每两个点之间有且只有一条有向边连接 求是否存在三元环 用拓扑排序判环,如果存在环,则一定存在三元环 证明如下: 不存在二元环 设存在 n(n>=3)元环 p1->p2->p3->…->pn->p1 1) 若存在边 p3->p1,则存在三元环 (p1->p2->p3->p1) 2) 若不存在 p3->p1,则必然存在 p1->p3

OpenGL Super Bible 7th - Drawing Our First Triangle(绘制第一个三角形)

简介 本文的原版为《OpenGL Super Bible 7th》,是同事给我的,翻译是原文+译文的形势。文章不属于机器直译,原因在于语言不存在一一对应的关系,我将尽可能的按照中国人看起来舒服的方式来翻译这些段子,如果段子让你感到身心愉悦,那还劳烦点个关注,追个更。如果我没有及时更新这些东西,那一定是我没好好干活导致的,欢迎同学们监督。 另外,我在东汉书院中为同学们准备了大量的游戏开发图形学方

FZU1669 Right-angled Triangle【毕达哥拉斯三元组】

题目链接: http://acm.fzu.edu.cn/problem.php?pid=1669 题目大意: 求满足以a、b为直角边,c为斜边,并且满足a + b + c <= L的直角三角形的个数。 思路: 勾股定理,a、b、c也就是本原毕达哥拉斯三元组,则满足: x = m^2 - n^2 y = 2*m*n z = m^2 + n^2 其中m > n,且若m为

从 poj 1163( The Triangle )教你彻底学会动态规划——入门篇

动态规划相信大家都知道,动态规划算法也是新手在刚接触算法设计时很苦恼的问题,有时候觉得难以理解,但是真正理解之后,就会觉得动态规划其实并没有想象中那么难。网上也有很多关于讲解动态规划的文章,大多都是叙述概念,讲解原理,让人觉得晦涩难懂,即使一时间看懂了,发现当自己做题的时候又会觉得无所适从。我觉得,理解算法最重要的还是在于练习,只有通过自己练习,才可以更快地提升。话不多说,接下来,下面

poj1163 The Triangle--动态规划入门(动态规划和贪心的去区别)

代码如下: #include<iostream>#include<algorithm>using namespace std;int D[101][101];int n;int maxSum[101][101];int main(){int i,j;cin >> n;for(i=0;i<n;i++)for(j=0;j<=i;j++){cin >> D[i][j];}for( int i

CodeForces 407A Triangle

太囧了。。。 最后代码中,把a/b*y1和a/b*x1改成a*y1/b和a*x1/b 大家都知道为什么。 思路是什么? 如果存在的话,其中一点肯定可以为(0,0),为什么? 因为,可以平移- -! 然后枚举一个点,求另一个点就可以了。 妈蛋,越想越气愤。为什么会卡在这里。 a/b在实际中是可以为小数的,但是根据,语言中的性质,a/b应该是一个整数。低级错误能不能不犯。? 还有就是,计算几