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("%lld",&n)){
        ansx.clear();
        ansy.clear();
    for(long long x=1;;x++)
    {
        long long A=x*(x+1);
        long long B=6*n/A;
        long long y=(B+x-1)/3;
        if(y<x) break;
        if((6*n)%A!=0) continue;
        if((B+x-1)%3!=0) continue;
        ansx.push_back(x);
        ansy.push_back(y);

    }

    int u;
    if(ansx[ansx.size()-1]==ansy[ansx.size()-1])
        u=2*ansx.size()-1;
    else u=2*ansx.size();
    printf("%d\n",u);
    for(int i=0;i<ansx.size();i++)
    {
        printf("%lld %lld\n",ansx[i],ansy[i]);
    }
    for(int i=ansx.size()-1;i>=0;i--)
    {
        if(ansy[i]==ansx[i]) continue;
        printf("%lld %lld\n",ansy[i],ansx[i]);
    }
    }
    //x*(x+1)(3*y-x+1)=6*n
    return 0;
}
时间: 2024-08-06 04:59:33

CodeForces 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

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() { #i

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 425D Sereja and Squares

题意: 平面上有n个点  问  最多能组成多少个边与坐标轴平行的正方形 思路: 这是一个通过不断二分查找乱搞的题- 首先枚举左下角  然后分别往上往右找左上角和右下角 这时如果发现边长不想等就通过长边长度在短边的方向二分查找最接近的值  不停往上往右延伸 如果发现边长想等了  那么要判断一下对应的左上角坐标出是不是有一个点 怎么判断呢  通过将所有点hash出一个值  然后二分- 反正这题就是各种二分乱搞 - -b  复杂度不好算  大概是n*(同x的点数+同y的点数) 代码: #include

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?×

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 994 C - Two Squares

C - Two Squares 思路: 点积叉积应用 代码: #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long #define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1

CodeForces 599B Spongebob and Joke

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=100000+10; int n,m; int b[maxn],f[maxn]; int cnt[maxn]; int main() { scanf("%d%d",&n,&m); memset(cnt,

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