Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)

http://codeforces.com/problemset/problem/599/D

题意:
给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值。

思路:

易得公式为:$\sum_{i=0}^{n}(n-i)(m-i) $

化简得:$\left [ n(n+1)-\frac{n(n+1)}{2}\right ]*m+\frac{n(n+1)(n+2)}{6}-\frac{n(n+1)}{2}*n$

将n作为小的数,枚举n即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<vector>
 4 #include<cmath>
 5 using namespace std;
 6
 7 typedef long long ll;
 8 typedef pair<ll,ll> pll;
 9
10 ll x;
11 vector<pll> ans1,ans2;
12
13 int main()
14 {
15     //freopen("in.txt","r",stdin);
16     ll tot = 0;
17     scanf("%lld",&x);
18     for(ll n=1;;n++)
19     {
20         ll a = n*(n+1) - n*(n+1)/2;
21         ll b = n*(n+1)*(2*n+1)/6 - n*n*(n+1)/2;
22         if(b > x)  break;
23         if((x  - b)%a == 0)
24         {
25             ll m = (x - b)/a;
26             if(m<n)  continue;
27             ans1.push_back(make_pair(n,m));
28             tot++;
29             if(m!=n)
30             {
31                 ans2.push_back(make_pair(m,n));
32                 tot++;
33             }
34         }
35     }
36     printf("%lld\n",tot);
37     for(int i=0;i<ans1.size();i++)
38     {
39         printf("%lld %lld\n",ans1[i].first,ans1[i].second);
40     }
41     for(int i=ans2.size()-1;i>=0;i--)
42     {
43         printf("%lld %lld\n",ans2[i].first,ans2[i].second);
44     }
45     return 0;
46 }
时间: 2024-10-02 23:27:20

Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)的相关文章

Codeforces Round #332 (Div. 2)D. Spongebob and Squares 数学

D. Spongebob and Squares 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 colum

Codeforces Round #332 (Div. 2) D. Spongebob and Squares

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?×

Codeforces Round #332 (Div. 二) B. Spongebob and Joke

Description While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not

Codeforces Round #332 (Div. 2) B. Spongebob and Joke 模拟

B. Spongebob and Joke While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1

Codeforces Round #332 (Div. 2)

好菜,不说话了,说题. A - Patrick and Shopping 从一个点出发,要经过其他两个点,然后回到原地,求最小时间花费.只有四种情况,从中选一个最小的就行了. #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <algorithm> using namespace

Codeforces Round #332 (Div. 2)A. Patrick and Shopping 水

A. Patrick and Shopping Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop an

2017-5-18-Train: Codeforces Round #332 (Div. 2)

A. Patrick and Shopping(模拟题) Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first sh

Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of s

Codeforces Round #506 (Div. 3) C. Maximal Intersection (枚举)

[题目描述] You are given $n$ segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide. The intersection of