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 × ...
随机推荐
- QGis 利用Python Console编写脚本进行批量处理
前言 这篇文章里,我们要完成一些数据的合并,计算等操作. 准备工作 首先要了解Qgis的编程模型,具体参考文章<QGIS里的编程模型>及<Qgis里的查询过滤>.了解了Qgis ...
- JedisConnectionException: Failed connecting to host localhost:6379
报错原因:没有启动服务,打开服务即可 redis.clients.jedis.exceptions.JedisConnectionException: Exception at redis.clien ...
- spring使用 hibernate jpa JpaRepository
使用JpaRepository需要两个架包: <dependency> <groupId>org.springframework.data</groupId> &l ...
- java字符串类型和时间类型的转换
类型转换 //reqeust.getParameter获取字符串直接赋值 1 public static Date date(String date_str) { try { Calendar zca ...
- TCP Flow Control and Data Transfer
TCP Flow Control TCP Data Transfer Selective Repeat ARQ with Positive ACK Window slides a byte basis ...
- Java 分支结构
Java 分支结构 - if...else/switch 顺序结构只能顺序执行,不能进行判断和选择,因此需要分支结构. Java 有两种分支结构: if 语句 switch 语句 if 语句 一个 i ...
- ucos问题
1. 在系统初始化之前,不要调用系统函数,如下: void OSRun(void) { SYSTICK_InternalInit(1); // 1ms time tick SYSTICK_IntCmd ...
- iOS11、iPhone X、Xcode9 适配指南
更新iOS11后,发现有些地方需要做适配,整理后按照优先级分为以下三类: 1.单纯升级iOS11后造成的变化: 2.Xcode9 打包后造成的变化: 3.iPhoneX的适配 一.单纯升级iOS11后 ...
- 1.Redis介绍以及安装
Redis介绍 Redis是一个开源的(BSD开源协议),内存数据结构存储,被用于作为数据库,缓存和消息代理. Redis支持如下数据结构: string(字符串) hashes(哈希) lists ...
- 类似"音速启动"的原创工具简码"万能助手"在线用户数终于突破100了!
原本只是开发出来方便自己的一个小工具,看到群友也喜欢,就随手分享了, 经过1个多月的自然积累,在线用户数终于突破100了,这增长速度实在让人泪奔~ 博客园的朋友如果看到,喜欢的话就拿去用吧, 万能助手 ...