codeforces 599D. Spongebob and Squares【推公式+暴力枚举】
2016-08-30 20:26
549 查看
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 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
output
input
output
input
output
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.
把③化简得到 :6*x = 6*n*n*m - 3*(n+m)*n*(n-1) + n*(n-1)*(2*n-1)
进而有 (3*n*n+3*n)*m = 6*x + n*n*n - n
枚举n求m即可
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
typedef long long ll;
using namespace std;
struct Node{
ll nn,mm;
};
vector<Node>ans;
ll high=5000000;
int main()
{
ll x;
scanf("%I64d",&x);
for(ll n=1;n<=high;n++){ //枚举n
ll a=6*x+n*n*n-n;
ll b=3*n*n+3*n;
if(a%b!=0)continue;
ll m=a/b;
if(n>m)break; //枚举到n<=m即可,因为在n>m时的结果等于n<m所有结果(n,m)的交换,即(m,n)
Node now;
now.nn=n;
now.mm=m;
ans.push_back(now);
}
int top=ans.size()-1;
//如果最末答案为(n,n),则总数为2*(n<=m的对数)-1,否则总数为2*(n<=m的对数)
if(ans[top].nn==ans[top].mm) top=ans.size()*2-1;
else top=ans.size()*2;
printf("%d\n",top);
int i;
top=ans.size()-1;
for(i=0;i<=top;i++){
printf("%I64d %I64d\n",ans[i].nn,ans[i].mm);
}
if(ans[top].nn==ans[top].mm)i=top-1;
else i=top;
for(;i>=0;i--){
printf("%I64d %I64d\n",ans[i].mm,ans[i].nn);
}
return 0;
}
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 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 262 9
3 5
5 3
9 226 1
input
2
output
21 22 1
input
8
output
4
1 82 3
3 28 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.
把③化简得到 :6*x = 6*n*n*m - 3*(n+m)*n*(n-1) + n*(n-1)*(2*n-1)
进而有 (3*n*n+3*n)*m = 6*x + n*n*n - n
枚举n求m即可
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
typedef long long ll;
using namespace std;
struct Node{
ll nn,mm;
};
vector<Node>ans;
ll high=5000000;
int main()
{
ll x;
scanf("%I64d",&x);
for(ll n=1;n<=high;n++){ //枚举n
ll a=6*x+n*n*n-n;
ll b=3*n*n+3*n;
if(a%b!=0)continue;
ll m=a/b;
if(n>m)break; //枚举到n<=m即可,因为在n>m时的结果等于n<m所有结果(n,m)的交换,即(m,n)
Node now;
now.nn=n;
now.mm=m;
ans.push_back(now);
}
int top=ans.size()-1;
//如果最末答案为(n,n),则总数为2*(n<=m的对数)-1,否则总数为2*(n<=m的对数)
if(ans[top].nn==ans[top].mm) top=ans.size()*2-1;
else top=ans.size()*2;
printf("%d\n",top);
int i;
top=ans.size()-1;
for(i=0;i<=top;i++){
printf("%I64d %I64d\n",ans[i].nn,ans[i].mm);
}
if(ans[top].nn==ans[top].mm)i=top-1;
else i=top;
for(;i>=0;i--){
printf("%I64d %I64d\n",ans[i].mm,ans[i].nn);
}
return 0;
}
相关文章推荐
- Codeforces 3 B. Lorry 暴力 排序 枚举 贪心
- Codeforces 599D Spongebob and Squares 【公式推导 暴力】
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
- Codeforces 355C Vasya and Robot 暴力枚举
- Codeforces 327A-Flipping Game(暴力枚举)
- codeforces 385d bear and floodlight【状压DP之暴力枚举】
- Codeforces 425A Sereja and Swaps(暴力枚举)
- codeforces 675B B. Restoring Painting(暴力枚举)
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
- !codeforces 399C Cards-数论&贪心-(暴力枚举)
- Codeforces 466B Wonder Room【思维+暴力枚举】直觉题
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
- Codeforces 372B. Counting Rectangles is Fun【动态规划,暴力枚举】(lowbit()小用法)
- codeforces 380B Sereja and Tree 暴力枚举
- Codeforces 426 B Sereja and Mirroring【暴力枚举+模拟】水题
- CodeForces 724B(暴力枚举)
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- Codeforces 828B Black Square【暴力枚举+二维前缀和】
- codeforces 700C Break Up 暴力枚举边+边双缩点(有重边)
- Codeforces-----233B---Non-square Equation暴力枚举