zoukankan      html  css  js  c++  java
  • CF 599D Spongebob and Squares(数学)

    题目链接:http://codeforces.com/problemset/problem/599/D

    题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个、2x2的方阵有8个、1X1的方阵有15个,所以F(3,5)=3+8+15=26。现在告诉你F(a,b)=x中的x,要你求出所有满足要求的a,b,并按a递增的顺序输出。

    思路:找出n行m列的矩阵中方阵数量的表达式即可,有些小细节需要注意。。。

    code:

     1 #include <iostream>
     2 #include <cmath>
     3 #include <algorithm>
     4 using namespace std;
     5 typedef long long LL;
     6 const int MAXN = 5000005;
     7 
     8 struct node {
     9     LL n;
    10     LL m;
    11 };
    12 
    13 node ans[MAXN];
    14 
    15 bool cmp(node a, node b)
    16 {
    17     return a.n < b.n;
    18 }
    19 
    20 int main()
    21 {
    22     LL x;
    23     while (cin >> x) {
    24         int k = 0;
    25         LL t = 1;
    26         LL p = (LL)sqrt(x);
    27         while (true) {
    28             if (t > 2000000 || t > p) break;
    29             LL a = 6*x - t + t*t*t;
    30             LL b = 3*t*(t+1);
    31             if (a%b == 0 && t<=a/b) {
    32                 ans[k].n = t;
    33                 ans[k++].m = a/b;
    34             }
    35             ++t;
    36         }
    37         int L = k;
    38         for (int i = k-1; i >= 0; --i) {
    39             if (ans[i].m == ans[i].n) continue;
    40             ans[L].n = ans[i].m;
    41             ans[L++].m = ans[i].n;
    42         }
    43         sort(ans, ans + L, cmp);
    44         cout << L << endl;
    45         for (int i = 0; i < L; ++i) {
    46             cout << ans[i].n << " " << ans[i].m << endl;
    47         }
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    php No input file specified 错误提示
    yii 隐藏index.php
    phpstudy 配置 sqlserver
    xdebug3 配置信息
    composer 更新
    mysql 命令行 导入大型文件
    使用 DBExportDoc V1.0 For MySQL 导出数据库表结构说明文档
    聚合数据接口调用实例
    servlet
    JDBC
  • 原文地址:https://www.cnblogs.com/ykzou/p/5016747.html
Copyright © 2011-2022 走看看