Codeforces Round #332 (Div. 2)-D Spongebob and Squares(枚举+递推)

海绵宝宝与方格谜题
本题探讨如何计算特定数量的独立正方形能够组成的矩形方格(n*m)的数量。通过数学公式推导和算法优化,实现了高效查找所有可能的组合。
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 15squares 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 is15 + 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.

题意:给你一个x,问你能组成多少种n*m的方格,使得铺上任意大小的正方形能铺满的方案数正好为x。

题解:我们枚举长和宽的话,复杂度一定会炸,我们可以考虑先假设n=m,呢此时是一个正方形方格,枚举出前几种方案数会发现规律,其实就是前n项的平方和,也就是tmp=1^2+2^2+...n^2=n*(n+1)*(2*n+1)/6.但此时并不一定能满足方案数正好为x。呢么就开始一层层加,每加一层你会发现方案数会多y=n+n-1+n-2+...+1=n*(n+1)/2,因此我们只需再判断(x-tmp)%y能否除尽即可,总复杂度为1e6

#include<set>      
#include<map>         
#include<stack>                
#include<queue>                
#include<vector>        
#include<string>     
#include<time.h>    
#include<math.h>                
#include<stdio.h>                
#include<iostream>                
#include<string.h>                
#include<stdlib.h>        
#include<algorithm>       
#include<functional>        
using namespace std;                
#define ll long long          
#define inf 1000000000           
#define mod 1000000007                
#define maxn  205000    
#define lowbit(x) (x&-x)                
#define eps 1e-9  
vector<pair<ll,ll> >q;
int main(void)
{
	ll n,i,tmp;
	scanf("%lld",&n);
	for(i=1;;i++)
	{
		tmp=i*(i+1)*(2*i+1)/6;
		if(tmp>n) break;
		ll now=n-tmp;
		ll x=i*(i+1)/2;
		if(now%x==0)
		{
			ll j=now/x;
			q.push_back(make_pair(i,i+j));
			if(j>0)
				q.push_back(make_pair(i+j,i));
		}
	}
	printf("%d\n",q.size());
	sort(q.begin(),q.end());
	for(i=0;i<q.size();i++)
		printf("%lld %lld\n",q[i].first,q[i].second);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值