zoukankan      html  css  js  c++  java
  • uva10177 (2/3/4)D Sqr/Rects/Cubes/Boxes?

    Problem J

    (2/3/4)-D Sqr/Rects/Cubes/Boxes?

    Input: standard input

    Output: standard output

    Time Limit: 2 seconds

    You can see a (4x4) grid below. Can you tell me how many squares and rectangles are hidden there? You can assume that squares are not rectangles. Perhaps one can count it by hand but can you count it for a (100x100) grid or a (10000x10000) grid. Can you do it for higher dimensions? That is can you count how many cubes or boxes of different size are there in a (10x10x10) sized cube or how many hyper-cubes or hyper-boxes of different size are there in a four-dimensional (5x5x5x5) sized hypercube. Remember that your program needs to be very efficient. You can assume that squares are not rectangles, cubes are not boxes and hyper-cubes are not hyper-boxes. 

    Fig: A 4x4 Grid                           Fig: A 4x4x4 Cube 

    Input

    The input contains one integer N (0<=N<=100) in each line, which is the length of one side of the grid or cube or hypercube. As for the example above the value of N is 4. There may be as many as 100 lines of input.

    Output

    For each line of input, output six integers S2, R2, S3, R3, S4, R4 in a single line where S2 means no of squares of different size in ( NxN) two-dimensional grid, R2 means no of rectangles of different size in (NxN) two-dimensional grid. S3, R3, S4, R4 means similar cases in higher dimensions as described before.  

     

    Sample Input:

    1
    2
    3

    Sample Output:

    1 0 1 0 1 0
    5 4 9 18 17 64
    14 22 36 180 98 1198


    Shahriar Manzoor

    “A bus was running at full speed and suddenly the driver stopped it.

    As a result a passenger fell down from his seat and began scolding

    Newton as Newton invented inertia of motion.”

     

     

    这道题目是找规律的数学题。

    数正方形很简单,就是 ∑ iw 求和, i 从1到n, w 是维数。

    长方形的话,以2维的为例,3,4维的类比一下就可以。

    拙劣的代码
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 #include <cmath>
     5 
     6 using namespace std;
     7 
     8 int main(void)
     9 {
    10     long long int s2, s3, s4, r2, r3, r4, temp;
    11     int n, i;
    12 
    13     while (cin >> n)
    14     {
    15         temp = n * (n + 1) / 2;
    16         s2 = n * (2 * n + 1) * (n + 1) / 6;
    17         r2 = temp * temp - s2;
    18         s3 = temp * temp;
    19         r3 = temp * s3 - s3;
    20         s4 = 0;
    21         for (i = 1; i <= n; i++)     s4 += i*i*i*i;
    22         r4 = s3 * s3 - s4;
    23         printf("%lld %lld %lld %lld %lld %lld\n", s2, r2, s3, r3, s4, r4);
    24     }
    25 
    26     return 0; 
    27 }

    假设一个正方形边长是n。

    从左边取一段作为长方形的一边,如果取长度为1,则有n种取法,如果长度为2,则有n-1种取法,以此类推,共有 n * (n + 1) / 2 种取法。

    从上边取一段作为长方形另一条临边,同理,有 n * (n + 1) / 2 种取法。

    令t = n * (n + 1) / 2,因此,长方形的个数一共有 t * t 个。

    其中包含了正方形,再把正方形减掉就可以了。 

    如果维数是 3 和 4 的话,则依次是 t * t * t, t * t * t * t ;

    另外,计算的时候,n 的平方求和可以用公式,n * (n + 1) * (2 * n + 1) / 2;

    n 的立方求和也有公式 n * n * (n + 1) * (n + 1) / 4。

    还有,要用 long long int 型变量。

    代码可读性不好,经过了一点简单的数学代换,所以直接看代码应该有点困难。知道方法自己推一下公式就可以了。

  • 相关阅读:
    Python绘图工具Plotly的简单使用
    gitlab runner安装与使用
    Ubuntu16.04下postgresql-10
    gitlab汉化
    Ubuntu 16.04 安装Gitlab
    Ubuntu-18.04安装Docker
    微信公众平台消息接口开发 彩票查询
    微信开发高级群发接口
    微信公众平台消息接口开发 快递查询
    搭建个人wordpress博客(小白教程)
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2755764.html
Copyright © 2011-2022 走看看