hackerrank Project Euler #210: Obtuse Angled Triangles
传送门
做出一个好几个星期屯下来的题目的感觉就是一个字:
爽!
上图的黄点部分就是我们需要求的点
两边的部分很好算
求圆的地方有一个优化,由于圆心是整数点,我们可以把圆分为下面几个部分,阴影部分最难算,最后乘就好了
代码如下所示
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
const int INF = 0x3f3f3f3f;
typedef long long ll;
typedef long double Double;
const Double tiny = 1e-20;
ll Ceil(Double x) {
ll tt = ceil(x);
if(abs(tt - x) < tiny) tt ++;
return tt;
}
ll Floor(Double x) {
ll tt = floor(x);
if(abs(tt - x) < tiny) tt --;
return tt;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
int r, a, b, n;
while(~scanf("%d %d %d %d", &r, &a, &b, &n)) {
Double leftEdge = Double(a*1.0)/b;
Double rightEdge = 2*n - Double(a*1.0)/b;
Double radiusTo2 = n - Double(a*1.0)/b;
if(leftEdge > rightEdge) swap(leftEdge, rightEdge);
ll sum = 0;
Double leftDouble = r + 2*leftEdge;
int leftInt = ceil(leftDouble);
Double rightDouble = r - 2*rightEdge;
int rightInt = ceil(rightDouble);
sum += 1ll * (rightInt + leftInt) * r;
if(rightInt % 2) sum += r & 1;
if(leftInt % 2) sum += r & 1;
// printf("%d %d %lld\n", leftInt, rightInt, sum);
Double cirRadius = (rightEdge - leftEdge) / sqrt(2);
// printf("%.3f\n", cirRadius);
ll tmpSum = 0;
for(int i = Floor(cirRadius), edge = ceil(radiusTo2); i >= edge; --i) {
tmpSum += Floor(sqrt( (rightEdge - leftEdge)*(rightEdge - leftEdge) / 2 - 1ll*i*i));
// tmpSum += Floor(sqrt( cirRadius * cirRadius - 1ll*i*i));
}
sum += tmpSum * 8;
// printf("%lld\n", sum);
sum += 1ll * Floor(cirRadius) * 4;
// printf("%lld\n", sum);
// printf("%.9f\n", (rightEdge - leftEdge)/2.0);
sum += 1ll* Floor(radiusTo2) * Floor(radiusTo2) * 4;
sum -= 1ll * Floor(n - leftEdge) * 2;
printf("%lld\n", sum);
}
return 0;
}
hackerrank Project Euler #210: Obtuse Angled Triangles的更多相关文章
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- Project Euler 91:Right triangles with integer coordinates 格点直角三角形
Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...
- Project Euler 39 Integer right triangles( 素勾股数 )
题意:若三边长 { a , b , c } 均为整数的直角三角形周长为 p ,当 p = 120 时,恰好存在三个不同的解:{ 20 , 48 , 52 } , { 24 , 45 , 51 } , ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
随机推荐
- 以ADO形式操作mysql数据库
首先得需要一个连接mysql的helper类: public class MySqlHelper { #region [ Connection ] public static string conne ...
- 十六、详述 IntelliJ IDEA 创建 Maven 项目及设置 java 源目录的方法
Maven 是一个优秀的项目管理工具,它为我们提供了一个构建完整的生命周期框架.现在,就让我们一起看看如何利用 IntelliJ IDEA 快速的创建 Maven 项目吧! 如上图所示,点击Creat ...
- [转] 各种Json解析工具比较 - json-lib/Jackson/Gson/FastJson
JSON技术的调研报告 一 .各个JSON技术的简介和优劣1.json-libjson-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,包括com ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 简单几行代码使用百度地图API接口分页获取信息
首发于: 万能助手扩展开发:使用百度地图API接口分页获取信息_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=426 使用 ...
- My collage goals
PART ONE: THE GOALS OF GRADE ONE 1, Try my best to improve my GPA , keep it around 4.0 2, Learn mor ...
- ubuntu查找端口和kill
查看 : netstat -anp | grep 8080 结束: kill -9 进程号
- 在SQL Server中批量修改有规律列的定义
)=N'要修改的表名'; --修改所有以sl结尾的列名的小数位数为4位 select syscolumns.name into #t1 from syscolumns,systypes where s ...
- Linux 只显示目录或者文件方法
ls 参数 -a 表示显示所有文件,包含隐藏文件-d 表示显示目录自身的属性,而不是目录中的内容-F 选项会在显示目录条目时,在目录后加一个/ 只显示目录 方法一: find . -type d -m ...
- 虚拟机(unbutun16.04)设置静态ip
电脑上装了虚拟机,想用xshell连接,无奈按照默认的网络设置方式每次重启了虚拟机后都要修改ip才能访问,这怎么能忍,经过一番折腾终于搞定这个问题了,解决步骤如下: 大步骤分为两步:其一是主机的设置, ...