Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25

20  7  8  9 10

19  6  1  2 11

18  5  4  3 12

17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

找规律。

右上角(2n+1)^2

左上角(2n+1)^2-(2n+1)+1

左下角(2n+1)^2-4*n

右下角(2n+1)^2-6*n

2n+1<=1001    n<=500

#include <iostream>
#include <string>
using namespace std; int main()
{
int n = 500;
long long res = 1;
for (int i = 1; i <= 500; i++)
{ int tmp = (2 * i + 1)*(2 * i + 1);
res += tmp*4 - (2 * i + 1) + 1 - 4 * i - 6 * i;
}
cout << res << endl;
system("pause");
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

Project Euler:Problem 28 Number spiral diagonals的更多相关文章

  1. Project Euler 28 Number spiral diagonals

    题意:给出一个 1001 × 1001 的矩阵,寻找每一圈四个顶点,并求出所有顶点的和 思路:只需要找到右上顶点数字的规律,然后每一圈四个顶点构成了一个等差数列,求个和即可 /************ ...

  2. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

  3. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  4. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  5. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  6. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  7. Project Euler:Problem 86 Cuboid route

    A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...

  8. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  9. Project Euler:Problem 61 Cyclical figurate numbers

    Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...

随机推荐

  1. C#添加水印

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  2. IE9下不显示select

    由于IE8和IE9下不兼容,需要在头部加入: <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7&q ...

  3. win32中SetCapture 和 ReleaseCapture的使用(查一下在VCL中的使用)

    最近在用win32写<visual C++经典游戏程序设计>中的扫雷游戏,在写到鼠标点击雷区的时候用到了SetCapture,和ReleaseCapture这对系统函数. 那么为什么需要用 ...

  4. ds finder 唤醒

    http://www.hangge.com/blog/cache/detail_594.html

  5. SpringBoot 使用 @Value 从 YAML文件读取属性(转)

    在 YAML中有如下配置 paypal: mode:live 在类中,通过 @Value属性读取 @Value("${paypal.mode}") private String m ...

  6. 使用Perl处理Excel之DMA映射

    使用Perl处理Excel之DMA映射 功能 通道处理,将各个通道的外设映射到通道上 外设ack信号处理 脚本执行情况 顶层Perl脚本(dma_parse.pl) 将上述两个功能脚本整合,便于调用 ...

  7. LSH︱python实现局部敏感随机投影森林——LSHForest/sklearn(一)

    关于局部敏感哈希算法.之前用R语言实现过,可是由于在R中效能太低.于是放弃用LSH来做类似性检索.学了python发现非常多模块都能实现,并且通过随机投影森林让查询数据更快.觉得能够试试大规模应用在数 ...

  8. [Compose] 18. Maintaining structure whilst asyncing

    We take our Promise.all() analogy further by using traversable on a Map(). Then we use two traversal ...

  9. Spring mvc 多文件上传

    http://blog.csdn.net/swingpyzf/article/details/20230865

  10. Android自定义控件View(二)继承控件

    在前一篇博客中学习了Android自定义控件View的流程步骤和注意点,不了解的童鞋可以参考Android自定义控件View(一).这一节开始学习自定义控件View(二)之继承系统已有的控件.我们来自 ...