Codeforces 599D Spongebob and Squares(枚举 + 数学)

@(K ACMer)

题意:给你一个数k,(k<1e18),表示矩形不同的正方形的个数,求满足k的所有矩形.

分析:求出公式,枚举实现即可.
这个题,比赛的时候错误的认知了复杂度,以为1e18必须要logn的算法才行,其实开次3根号也好的.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <string>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
#define xx first
#define yy second
const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e5 + 41;
ull k;
vector<pair<ull, ull> > v;
pair<ull, ull> temp;
int main(void) {
    cin >> k;
    ull a = 1, b = 0;
    ull i = 0;
    while (++i) {
        a = i * (i + 1) / 2;
        b = b + (i - 1) * i / 2;
        temp.xx = i, temp.yy = (k + b) / a;
        if (temp.xx > temp.yy) break;
        if ((k + b) % a == 0) v.push_back(temp);
    }
    int ans = (int)v.size() * 2;
    if (v[v.size() - 1].yy == v[v.size() - 1].xx) ans--;
    cout << ans << endl;
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].xx << " " << v[i].yy << endl;
    }
    for (int i = (int)v.size() - 1; i >= 0; i--) {
        if (v[i].yy == v[i].xx) continue;
        cout << v[i].yy << " " << v[i].xx << endl;
    }
    return 0;
}



你可能感兴趣的:(codeforces)