求一个大小是N的正方形中正方形的个数S2,长方形的个数R2;
求一个棱长为N的正方体中正方体的个数S3,长方体的个数R3;
求一个边长为N的超正方体中超正方体的个数S4,超长方体的个数R4;
在以上结果中正方形不视为长方形。。。
首先对于S2和R2,
显然S2的个数为n*n + (n - 1)*(n - 1) + (n - 2)*(n - 2) + ... + 1*1 即边长为k的正方形的个数为 (n + 1 - k) * (n + 1 - k) 个
对于R2首先考虑整个正方形中可以得到的矩形的个数,考虑矩形左上角对应的是原正方形中的哪个,而右下角的对应的又是哪个
则可以知道对于以第 i 行 j 列的正方形作为左上角的矩形有 (n + 1 - i)*(n + 1 - j) 种;
那么 R2 = sigma(sigma(i*j)) - sigma(i*i) = n*n*(n + 1)*(n + 1) / 4 - n * (n + 1) * (2*n + 1) / 6
类似的可以知道 S3 = sigma(i*i*i) = n*n*(n + 1)*(n + 1) / 4;
R3 = sigma(sigma(sigma(i*j*k))) - sigma(i*i*i) = (n*(n + 1)/2)^3 - n*n*(n + 1)*(n + 1) / 4;
S4 = sigma(i*i*i*i) = n * (n + 1) * (2*n + 1) * (3*n*n + 3*n - 1) / 30
R4 = sigma(sigma(sigma(sigma(i*j*k*r)))) - sigma(i*i*i*i) = (n * (n + 1) / 2)^4 - S4;
代码如下:
Result : Accepted Memory : 0 KB Time : 19 ms
/* * Author: Gatevin * Created Time: 2014/7/7 21:36:38 * File Name: test.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; int N; long long S2[101]; long long R2[101]; long long S3[101]; long long R3[101]; long long S4[101]; long long R4[101]; int main() { memset(S2, 0, sizeof(S2)); memset(R2, 0, sizeof(R2)); memset(S3, 0, sizeof(S3)); memset(R3, 0, sizeof(R3)); memset(S4, 0, sizeof(S4)); memset(R4, 0, sizeof(R4)); for(long long i = 0; i <= 100; i++) { long long tmp = (i * (i + 1)) / 2; S2[i] = (i * (i + 1) * (2*i + 1)) / 6; S3[i] = tmp * tmp; R2[i] = S3[i] - S2[i]; R3[i] = S3[i]*tmp - S3[i]; S4[i] = (i * (i + 1) * (2*i + 1) * (3 * i * i + 3 * i - 1)) / 30; R4[i] = S3[i]*S3[i] - S4[i]; } while(cin>>N) { cout<<S2[N]<<" "<<R2[N]<<" "<<S3[N]<<" "<<R3[N]<<" "<<S4[N]<<" "<<R4[N]<<endl; } return 0; }