Heron and His Triangle(HDU 6222 找规律+大数)
来源:互联网 发布:网络填表终结者 编辑:程序博客网 时间:2024/05/16 10:42
Heron and His Triangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 751 Accepted Submission(s): 364
Problem Description
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.
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).
Output
For each test case, output the smallest t in a line. If the Heron’s triangle required does not exist, output -1.
Sample Input
41234
Sample Output
4444
Source
2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
//题意:
给定一个n, 求t-1,t,t+1作为3条边构成的三角形面积为正整数,t>=n,求最小t 。
//思路:
三角形面积 S=(1/2)*a*b*sin<a,b> ;
cos<a,b> = (a^2+b^2-c^2)/(2*a*b) ;
sin<a,b>^2+cos<a,b>^2=1 ;
a=a; b=a+1; c=a+2;
=>S = (a+1)*sqrt(3*(a-1)*(a+3))/4 ; S要为正整数 ;
=>写个小程序枚举满足条件的t,得:t = 4, 14, 52, 194, 724 ...
=>规律: t[i]=t[i-1]*4-t[i-2] ;
按照上述规律,大数打表即可:由于t大致是按照4的指数上升的,所以10^32内,t大致是150个左右。
//题意:
给定一个n, 求t-1,t,t+1作为3条边构成的三角形面积为正整数,t>=n,求最小t 。
//思路:
三角形面积 S=(1/2)*a*b*sin<a,b> ;
cos<a,b> = (a^2+b^2-c^2)/(2*a*b) ;
sin<a,b>^2+cos<a,b>^2=1 ;
a=a; b=a+1; c=a+2;
=>S = (a+1)*sqrt(3*(a-1)*(a+3))/4 ; S要为正整数 ;
=>写个小程序枚举满足条件的t,得:t = 4, 14, 52, 194, 724 ...
=>规律: t[i]=t[i-1]*4-t[i-2] ;
按照上述规律,大数打表即可:由于t大致是按照4的指数上升的,所以10^32内,t大致是150个左右。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <algorithm>using namespace std;char num[500][40];//大数乘法void mul(char a[], int x,char (&res)[40]){ int len=strlen(a); int flag=0; char ans[40]; for(int i=len-1;i>=0;i--) { int val=(a[i]-'0')*4+flag; ans[i]=val%10+'0'; flag=val/10; } ans[len]='\0'; res[0]='\0'; if(flag) { res[0]=flag+'0'; res[1]='\0'; strcat(res,ans); } else { strcpy(res,ans); }}//大数减法void sub(char (&a)[40], char b[]){ char ans[40]; char tmp[40]; int lena=strlen(a); int lenb=strlen(b); for(int i=0;i<lena-lenb;i++) { ans[i]='0'; } ans[lena-lenb]='\0'; strcat(ans,b); strcpy(tmp,b); strcpy(b,ans); int len=max(lena,lenb); int flag=0; memset(ans,0,sizeof(ans)); for(int i=len-1;i>=0;i--) { int val=a[i]-b[i]-flag; if(val<0) { ans[i]=val+10+'0'; flag=1; } else { ans[i]=val+'0'; flag=0; } } ans[len]='\0'; strcpy(a,ans); strcpy(b,tmp);}int judge(char a[]){ int len=strlen(a); if(len>=32) return 1; else return 0;}//比较两个数大小int compare(char a[], char b[]){ int lena=strlen(a); int lenb=strlen(b); if(lena>lenb) return 1; else if(lena<lenb) return 0; for(int i=0;i<lena;i++) { if(a[i]>b[i]) return 1; else if(a[i]<b[i]) return 0; } return 2;}int main(){ int T; char n[40]; //打表 strcpy(num[0],"4"); strcpy(num[1],"14"); for(int i=2;;i++) { mul(num[i-1],4,num[i]); sub(num[i],num[i-2]); if(judge(num[i])) break; } scanf("%d",&T); while(T--) { scanf("%s",n); if(strcmp(n,"1")==0||strcmp(n,"2")==0||strcmp(n,"3")==0||strcmp(n,"4")==0) { printf("4\n"); continue; } int flag=0; int cnt; for(int i=0;;i++) { int x=compare(n,num[i]); if(flag==1&&x!=1) { cnt=i; break; } if(x==1) flag=1; } printf("%s\n",num[cnt]); } return 0;}
阅读全文
0 0
- Heron and His Triangle(HDU 6222 找规律+大数)
- hdu 6222 Heron and His Triangle
- Heron and His Triangle 规律+java大整数
- 【HDU 6222 2017ACM/ICPC亚洲区沈阳站】Heron and His Triangle
- hdu-1297(找规律+大数加法)
- hdu 5047 大数找规律
- Codeforces 66 D Petya and His Friends(找规律)
- BZOJ1002(找规律+大数)
- HDU 5047 Sawtooth (大数模板+找规律)
- hdu (找规律)
- UVa 11401 Triangle Countin(找规律)
- HDU5914 Triangle (找规律)
- HDU 5559 Frog and String (构造+找规律)
- HDU-5810-Balls and Boxes-数学推导(找规律)
- HDU 5914 Triangle【找规律】【2016中国大学生程序设计竞赛(长春)】
- HDU 5914 Triangle【斐波那契 找规律】
- 周赛 大数找规律
- HDU 1297(大数+规律)
- JAVA8学习总结——函数式编程与lambda表达式
- 文章标题
- 三十分钟学会 Less
- fork之后子进程到底复制了父进程什么
- 最长公共子序列(LCS)
- Heron and His Triangle(HDU 6222 找规律+大数)
- Apache有几种工作模式,分别介绍下其特点,并说明什么情况下采用的工作模式?
- 大数减法
- LeetCode刷题记录6-查找字符串的最长串
- C++避免变量重复定义
- MOOC人工智能原理学习笔记2
- c++有序关联容器中键类型的约束
- BZOJ1818 [Cqoi2010]内部白点 扫描线/线段求交
- 微信的一次性订阅消息