CF 599D Spongebob and Squares(数学)

题目链接:http://codeforces.com/problemset/problem/599/D

题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个、2x2的方阵有8个、1X1的方阵有15个,所以F(3,5)=3+8+15=26。现在告诉你F(a,b)=x中的x,要你求出所有满足要求的a,b,并按a递增的顺序输出。

思路:找出n行m列的矩阵中方阵数量的表达式即可,有些小细节需要注意。。。

code:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 const int MAXN = 5000005;
 7 
 8 struct node {
 9     LL n;
10     LL m;
11 };
12 
13 node ans[MAXN];
14 
15 bool cmp(node a, node b)
16 {
17     return a.n < b.n;
18 }
19 
20 int main()
21 {
22     LL x;
23     while (cin >> x) {
24         int k = 0;
25         LL t = 1;
26         LL p = (LL)sqrt(x);
27         while (true) {
28             if (t > 2000000 || t > p) break;
29             LL a = 6*x - t + t*t*t;
30             LL b = 3*t*(t+1);
31             if (a%b == 0 && t<=a/b) {
32                 ans[k].n = t;
33                 ans[k++].m = a/b;
34             }
35             ++t;
36         }
37         int L = k;
38         for (int i = k-1; i >= 0; --i) {
39             if (ans[i].m == ans[i].n) continue;
40             ans[L].n = ans[i].m;
41             ans[L++].m = ans[i].n;
42         }
43         sort(ans, ans + L, cmp);
44         cout << L << endl;
45         for (int i = 0; i < L; ++i) {
46             cout << ans[i].n << " " << ans[i].m << endl;
47         }
48     }
49     return 0;
50 }
posted @ 2015-12-03 16:39  jasaiq  阅读(242)  评论(0编辑  收藏  举报