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. JavaScript系列--JavaScript数组高阶函数reduce()方法详解及奇淫技巧

    一.前言 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. reduce()方 ...

  2. HDU 1874 畅通工程续 SPFA || dijkstra||floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 给你一些点,让你求S到T的最短路径. 我只是来练习一下SPFA的 dijkstra+邻接矩阵 ...

  3. JAVA初始开发环境搭建

    上午想在一台新电脑上搭建java开发环境,在没有之前备份的情况下,单靠网络还真有点麻烦.最主要的原因是貌似在我当前的网络环境下jdk无法下载,官网这个链接半天打不开,http://www.oracle ...

  4. View的事件分发机制解析

    引言 Android事件构成 在Android中,事件主要包含点按.长按.拖拽.滑动等,点按又包含单击和双击,另外还包含单指操作和多指操作.全部这些都构成了Android中的事件响应.总的来说.全部的 ...

  5. js循环一维数组按指定长度截取为二维数组

    //随便创建一个数组 let data = "abcdefghijklmnopkrstuvw12322999".split(""); //总数组 let pro ...

  6. Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏

    以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...

  7. js判断是否微信客户端

    上周接到个需求,需求是这样的:用户扫一扫二维码会产生一个链接,该链接会向后端发送个请求,返回一个 apk 的下载地址,用户点击下载按钮可以下载此 apk.然后就发生了问题,经过测试,发现用微信扫一扫打 ...

  8. 无线物联网中CoAP协议的研究与实现【转】

    无线物联网中CoAP协议的研究与实现 时间:2013-04-09 来源:电子科技 作者:汤春明,张 荧,吴宇平 关键字:CoAP   无线   物联网   协议 摘要:由于物联网中的很多设备都是资源受 ...

  9. Windows 程序启动性能优化(先载入EXE,后载入DLL,只取有限的代码载入内存,将CPU的IP指向程序的入口点)

    一.重定位链接时重定位:目标文件一般由多个节组成,编译器在编译每个目标文件时一般都是从0地址开始生成代码.当多个代码节合成一个代码段时,需要根据其在最终代码段中的位置做出调整.同时,链接器需要对已经解 ...

  10. FireFox中使用showModalDialog显示的对话框不能用window.close()关闭

    FireFox中使用 showModalDialog 显示的对话框不能用 window.close() 进行关闭: 是FireFox的配置问题,解决方法如下: 在FireFox地址栏里输入 about ...