codeforces #332 div 2 D. Spongebob and Squares

http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的时候gg了。。其实稍微在纸上推一下。就会得到对于n,m的矩形,一共会有-nnn+3nnm+n+3n*m的方格。数量级是n3。 我们可以实际跑一遍。发现对于x1E18的数量级,n不会超过1442550,1E6,可以搞。

需要注意的是,一个是会爆int,所以记得用long long

另一个是如果两个数相等,记得只输入一组,并且方案数-1

我是用set +pair存的答案。。反向遍历set的时候要用reserve_iterator...

/* ***********************************************
Author :111qqz
Created Time :2015年12月23日 星期三 15时54分37秒
File Name :code/cf/#332/D.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < LL ,LL >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6LL x;
 7LL cur;
 8const LL MAXN = 1442550;
 9set<pi>se;
10LL cal( LL n,LL m)
11{
12    LL res = -n*n*n+3*n*n*m+3*n*m+n;
13    return res;
14}
15int square = -1;
16int main()
17{
18	#ifndef  ONLINE_JUDGE 
19	freopen("code/in.txt","r",stdin);
20  #endif
21//	for ( LL i = 1 ; ; i++)
22//	{
23//	    cur = cal(i,i);
24//	  //  cout<<"i:"<<i<<" cur:"<<cur<<endl;
25//	    if (cur>1E18)
26//	    {
27//		cout<<i<<endl;
28//		break;
29//	    }
30//
31//	}
32	cin>>x;
33	x = x*6;
34	for ( LL i = 1 ; cal(i,i)<=x ; i++)
35	{
36	    LL tmp = (x-i+i*i*i)%(3*i*i+3*i);
37	  //  cout<<"i:"<<i<<" tmp:"<<tmp<<endl;
38	    if (tmp==0)
39	    {
40		se.insert(make_pair(i,(x-i+i*i*i)/(3*i*i+3*i)));
41		if (i==(x-i+i*i*i)/(3*i*i+3*i)) square = 1;
1	    }
2	}
3	if (square==1)
4	cout<<se.size()*2-1<<endl;
5	else cout<<se.size()*2<<endl;
6	set<pi>::iterator it;
7	for (it = se.begin(); it!=se.end() ;it++)
8	{
 1	    cout<<(*it).fst<<" "<<(*it).sec<<endl;
 2	}
 3	set<pi>::reverse_iterator it2;   //反向遍历要用反向迭代器。。。右忘记了。。。。
 4	for ( it2 =se.rbegin() ;it2 !=se.rend() ; it2 ++)
 5	{
 6	    if (square==1)
 7	    {
 8		square = 2;
 9		continue;
10	    }
	    cout<<(*it2).sec<<" "<<(*it2).fst<<endl;
	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}