In mathematics, a polygonal number is a number represented as dots or pebbles arranged in the shape of a regular polygon. The dots are thought of as alphas (units). These are one type of 2-dimensional figurate numbers. The following picture shows how triangular numbers, square numbers, pentagonal numbers and hexagonal numbers represented as dots arranged in the shape of corresponding regular polygon.
2016 is not only a leap year but also a triangular and hexagonal year. If you are patient enough, you can count the number of the dots in the left triangle or in the right hexagon in the following picture. The number of dots in each shape is 2016.
Therefore, 2016 is a triangular-hexagonal-leap year. The previous triangular-hexagonal-leap year is 1540 and the next is 2556. So living to see 2016 is very rare experience.
You task is to list the triangular-hexagonal-leap years from 2016 to 990528. 990528 is also a triangular-hexagonal-leap year.
Input
This problem has no input.
Output
Please print each triangular-hexagonal-leap year in increasing order.
For example, if you are asked to list the triangular-hexagonal-leap years from 780 to 2556, the output should be:
780 1128 1540 2016 2556
Sample Output
2016 2556 ... <-- some lines are skipped 990528
思路:这道题对于我这个数学很不好的人来说,真的很骚,我冥思苦想,也想不出是这样一个思路。
(三角形)2016 = 63*64/2,(六边形)2016 = 32*63,找在三角形和六边形中共同出现的那个数,用map存起来,再输出共同部分。
#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <queue> #include <algorithm> #include <map> using namespace std; #define ll long long const int inf = 0xffffff; const int maxn = 1e6; bool cmp(int x) { if((x%4 == 0 && x%100 != 0) || (x%400 == 0)) return 1; return 0; } int main() { map<int, int>a[2]; for(int i = 63; i*(i+1) <= 990528*2; i++)//把三角形的n*(n+1)/2存起来 { a[0][i*(i+1)/2] = 1; } for(int i = 32; i*(2*i-1) <= 990528; i++)//把六边形的n*(3*n-1)/2 { a[1][i*(2*i-1)] = 1; } map<int, int>::iterator i; for(i = a[0].begin(); i != a[0].end(); i++)//找三角形和六边形共同出现的数字 if(a[1][i->first] && cmp(i->first)) printf("%d ", i->first); return 0; }