Codeforces 599D Spongebob and Squares【思维枚举+数学方程】

D. Spongebob and Squares
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 × 5 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3 × 5 table is 15 + 8 + 3 = 26.

Input

The first line of the input contains a single integer x (1 ≤ x ≤ 1018) — the number of squares inside the tables Spongebob is interested in.

Output

First print a single integer k — the number of tables with exactly x distinct squares inside.

Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality — in the order of increasing m.

Examples
Input
26
Output
6
1 26
2 9
3 5
5 3
9 2
26 1
Input
2
Output
2
1 2
2 1
Input
8
Output
4
1 8
2 3
3 2
8 1
Note

In a 1 × 2 table there are 2 1 × 1 squares. So, 2 distinct squares in total.

In a 2 × 3 table there are 6 1 × 1 squares and 2 2 × 2 squares. That is equal to 8 squares in total.



题目大意:

让你求一共有多少种方案,使得n*m的正方形中,有k个子正方形。


思路:


1、首先很明显,对于一个3*5的正方形中,其有子正方形:n*m+(n-1)*(m-1)+(n-2)*(m-2)==26个;

那么同理,对于一个n*m的正方形中,其有子正方形:

tmp=min(n,m);

数量=n*m*tmp-【(tmp)*(tmp-1)/2】*(x+y)+1^2+2^2+3^2+4^2+..........(tmp-1)^2;

那么问题就转化成为:有多少个x,使得n*m*tmp-【(tmp)*(tmp-1)/2】*(n+m)+1^2+2^2+3^2+4^2+..........(tmp-1)^2==X.


2、接下来观察等式,等式右侧一定是大于等于1的一个数,那么肯定一点,如果1^2+2^2+..........+(tmp-1)^2大于了X.那么显然就是无解的。

那么我们着重讨论1^2+2^2+.............+(tmp-1)^2.

打个暴力的表,并且存到sum【i】,表示1^2+2^2+.........+i^2的值。

当sum【i】>1e18的时候跳出,不难发现,这个数组的长度是1e6+的。

那么我们可以考虑枚举n,同时设定tmp==n,对于剩下已知部分,很容易就能求得m。

接下来的任务就变成了解方程式,求y.

对于求出的解进行统计即可。


3、注意n==m的时候,千万不能多输出。(被这里坑了一波,很蓝廋);


Ac代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll __int64
struct node
{
    ll x,y;
}ans[1442275];
ll sum[1442275];
int tot;
int cmp(node a,node b)
{
    return a.x<b.x;
}
void init()
{
    ll summ=0;
    for(ll i=1;i<=100000000;i++)
    {
        summ+=i*i;
        if(summ>=1000000000000000000)
        {
            tot=i-1;
            break;
        }
        sum[i]=summ;
    }
}
int main()
{
    init();
    ll x;
    while(~scanf("%I64d",&x))
    {
        int cnt=0;
        for(ll i=1;i<=tot;i++)
        {
            if(x>=sum[i])
            {
                ll right=x-sum[i-1];
                right+=(i-1)*i*i/2;
                right*=2;
                if(right%(i*i+i)==0)
                {
                    ll y=right/(i*i+i);
                    ans[cnt].x=i;
                    ans[cnt++].y=y;
                    ans[cnt].x=y;
                    ans[cnt++].y=i;
                    if(i==y)cnt--;
                }
            }
        }
        sort(ans,ans+cnt,cmp);
        printf("%d\n",cnt);
        for(int i=0;i<cnt;i++)
        {
            printf("%I64d %I64d\n",ans[i].x,ans[i].y);
        }
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值