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 × ...
随机推荐
- byte转换字符串(string)+字符串转换byte
C# 中字符串string和字节数组byte[]的转换 //string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetByte ...
- 相机姿态估计(Pose Estimation)
(未完待续.....) 根据针孔相机模型,相机成像平面一点的像素坐标p和该点在世界坐标系下的3D坐标P有$p=KP$的关系,如果用齐次坐标表示则有: $$dp=KP$$ 其中d是空间点深度(为了将p的 ...
- x+=i和x = x+i比较 -- 简单赋值和复合赋值
这两个赋值方式其实是有区别的,如果最后结果的类型和左操作数的类型一样,那么这两个表达式就完全等价. 下面来看看两个例子来理解它们的区别: 编写一个程序,使得x+=i合法, x = x+i: 不合法. ...
- Xcode-push到远程仓库不能使用邮箱名,需使用昵称
1.Xcode-push到远程仓库不能使用邮箱 2.Xcode-push到远程仓库需使用昵称
- 去掉CodeIgniter URL中的index.php
CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost /CodeIgniter/index ...
- one or more listeners failed to start问题解决思路
今日搭建一个web应用的时候总是遇到tomcat报错:one or more listeners failed to start. Full detail balabale....而且还没有其他提示, ...
- 我的前端工具集(七)div背景网格
我的前端工具集(七)div背景网格 liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 有时候总觉得div颜色过于白,于是给了10%的灰 但是并不一定能解决问题,因为页面中会有不均衡的 ...
- CSS实现图片等比例缩小不变形
<img src="../images/bg1.jpg" alt="" /> img { /*等宽缩小不变形*/ /*width: 100%;*/ ...
- [异常笔记]启动DFS报错:Cannot find configuration directory: /etc/hadoop
[hadoop@master ~]$ start-dfs.sh Incorrect configuration: namenode address dfs.namenode.servicerpc-ad ...
- 《MySQL必知必会》--使用cmd登陆数据库
数据库:保存有组织的数据的容器(通常是一个文件或一组文件). 表:某种特定类型数据的结构化清单. 模式:关于数据库和表的布局及特性的信息. 列:表中的一个字段.所有表都是由一个或多个列组成的. 数据类 ...