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…

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月23日 星期三 15时54分37秒
 4File Name :code/cf/#332/D.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < LL ,LL >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL x;
34LL cur;
35const LL MAXN = 1442550;
36set<pi>se;
37LL cal( LL n,LL m)
38{
39    LL res = -n*n*n+3*n*n*m+3*n*m+n;
40    return res;
41}
42int square = -1;
43int main()
44{
45	#ifndef  ONLINE_JUDGE 
46	freopen("code/in.txt","r",stdin);
47  #endif
48//	for ( LL i = 1 ; ; i++)
49//	{
50//	    cur = cal(i,i);
51//	  //  cout<<"i:"<<i<<" cur:"<<cur<<endl;
52//	    if (cur>1E18)
53//	    {
54//		cout<<i<<endl;
55//		break;
56//	    }
57//
58//	}
59	cin>>x;
60	x = x*6;
61	for ( LL i = 1 ; cal(i,i)<=x ; i++)
62	{
63	    LL tmp = (x-i+i*i*i)%(3*i*i+3*i);
64	  //  cout<<"i:"<<i<<" tmp:"<<tmp<<endl;
65	    if (tmp==0)
66	    {
67		se.insert(make_pair(i,(x-i+i*i*i)/(3*i*i+3*i)));
68		if (i==(x-i+i*i*i)/(3*i*i+3*i)) square = 1;
69
70	    }
71	}
72	if (square==1)
73	cout<<se.size()*2-1<<endl;
74	else cout<<se.size()*2<<endl;
75	set<pi>::iterator it;
76	for (it = se.begin(); it!=se.end() ;it++)
77	{
78
79	    cout<<(*it).fst<<" "<<(*it).sec<<endl;
80	}
81	set<pi>::reverse_iterator it2;   //反向遍历要用反向迭代器。。。右忘记了。。。。
82	for ( it2 =se.rbegin() ;it2 !=se.rend() ; it2 ++)
83	{
84	    if (square==1)
85	    {
86		square = 2;
87		continue;
88	    }
89
90
91	    cout<<(*it2).sec<<" "<<(*it2).fst<<endl;
92	}
93
94
95  #ifndef ONLINE_JUDGE  
96  fclose(stdin);
97  #endif
98    return 0;
99}