codeforce 599D Spongebob and Squares

很容易得到n × m的方块数是

然后就是个求和的问题了,枚举两者中小的那个n ≤ m。

然后就是转化成a*m + c = x了。a,m≥0,x ≥ c。最坏是n^3 ≤ x,至于中间会不会爆,测下1e18就好。

#include<bits/stdc++.h>
using namespace std;

typedef long long ull;

vector<ull> ns;
vector<ull> ms;

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    ull x, t, c, a, n, m; cin>>x;
    int k = 0;
    //ns.push_back(1); ms.push_back(x);
    int equ = 0;
    for(n = 1;  ; n++){
        t = n*(n+1)/2;
        c = (n)*(n+1)*(2*n+1)/6 - n*t;
        if(c > x) break;
        a = n*(n+1) - t;
        if((x - c) % a == 0) {
            m = (x-c)/a;
            if(m < n) break;
            ns.push_back(n);
            ms.push_back(m);
            k++;
            if(m == n){
                equ = 1; break;
            }
        }
    }

    if(equ){
        k = 2*k-1;
        printf("%d\n", k);
        int sz = ns.size();
        for(int i = 0; i < sz; i++){
            printf("%I64d %I64d\n", ns[i], ms[i]);
        }
        for(int i = sz-2; i >= 0; i--){
            printf("%I64d %I64d\n", ms[i], ns[i]);
        }
    }
    else {
        k = 2*k;
        printf("%d\n", k);
        int sz = ns.size();
         for(int i = 0; i < sz; i++){
            printf("%I64d %I64d\n", ns[i], ms[i]);
        }
        for(int i = sz-1; i >= 0; i--){
            printf("%I64d %I64d\n", ms[i], ns[i]);
        }
    }

    return 0;
}
时间: 2024-11-03 05:21:42

codeforce 599D Spongebob and Squares的相关文章

[cf 599D] Spongebob and Squares

据题意: k=Σ(i=0 to n-1)(n-i)*(m-i) k=n2m -(n+m)Σ(i)+Σ(i2) 展开化简 m=(6k-n+n3)/(3n2+3n) 枚举n,验证整除,只做n<=m,其余反过来输出即可 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath

CodeForces 599D Spongebob and Squares

先推导出公式x*(x+1)(3*y-x+1)=6*n,然后枚举求解. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; vector<long long >ansx,ansy; long long n; int main() { while(~scanf("%

Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学

D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m colum

Codeforces Round #332 (Div. 2) D. Spongebob and Squares

Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3?×

codeforce 599B Spongebob and Joke

一道水题WA那么多发,也是醉了.f看成函数的话,其实就是判断一下反函数存不存在. 坑点,只能在定义域内判断,也就是只判断b[i].没扫一遍前不能确定Impossible. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+10; int f[maxn], b[maxn], a[maxn]; int g[maxn]; bool mul[maxn]; //#define L

Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)

http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\sum_{i=0}^{n}(n-i)(m-i) $ 化简得:$\left [ n(n+1)-\frac{n(n+1)}{2}\right ]*m+\frac{n(n+1)(n+2)}{6}-\frac{n(n+1)}{2}*n$ 将n作为小的数,枚举n即可. 1 #include<iostream>

Codeforces Round #332 (Div. 2)

好菜,不说话了,说题. A - Patrick and Shopping 从一个点出发,要经过其他两个点,然后回到原地,求最小时间花费.只有四种情况,从中选一个最小的就行了. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace

Codeforce 22B Bargaining Table

B. Bargaining Table Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n?×?m meters. Each square meter of the room is either occupied by so

USACO 1.2 Palindromic Squares

Palindromic SquaresRob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that