BZOJ 1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪(母函数)
1643: [Usaco2007 Oct]Bessie’s Secret Pasture 贝茜的秘密草坪
Time Limit: 5 Sec Memory Limit: 64 MB Submit: 330 Solved: 278 [Submit][Status][Discuss]
Description
农夫约翰已经从他的牧场中取得了数不清块数的正方形草皮,草皮的边长总是整数(有时农夫约翰割草皮的刀法不合适,甚至切出了边长为0的正方形草皮),他已经把草皮放在了一个奶牛贝茜已经知道的地方。 贝茜总是希望把美味的草皮放到她的秘密庄园里,她决定从这些草皮中取出恰好4块搬到她的秘密庄园中,然后把它们分成1×1的小块,组成一个面积为N(1<=N<=10,000)个单位面积的部分。 贝茜对选出这样四块草皮的方法数很感兴趣,如果她得到了一个4个单位面积的部分,那么她可以有5中不同的方法选4块草皮:(1,1,1,1),(2,0,0,0),(0,2,0,0),(0,0,0,2).顺序是有效的:(4,3,2,1)和(1,2,3,4)是不同的方法。
Input
第一行:一个单独的整数N。
Output
单独的一行包含一个整数,表示贝茜选四块草皮的方案数。
Sample Input
4
Sample Output
5
思路:母函数。把四个位置看作四个式子。每个式子的i可以取从0到i*i<=n的最大值。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年04月10日 星期日 16时34分31秒
4File Name :code/bzoj/1643.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E4+7;
34int n;
35int a[N],tmp[N];
36int main()
37{
38 #ifndef ONLINE_JUDGE
39 freopen("code/in.txt","r",stdin);
40 #endif
41
42 scanf("%d",&n);
43 ms(tmp,0);
44 for ( int i = 0 ; i*i <= n ; i++)
45 {
46 a[i*i] = 1;
47 }
48 for ( int i = 2 ; i <= 4 ; i++)
49 {
50 for ( int j = 0 ; j <= n ; j++)
51 {
52 for ( int k = 0 ; k*k+j <= n ; k++)
53 {
54 tmp[j+k*k] += a[j];
55 }
56 }
57
58 for ( int j = 0 ; j <= n ; j++)
59 {
60 a[j] = tmp[j];
61 tmp[j] = 0 ;
62 }
63 }
64
65 printf("%d\n",a[n]);
66
67 #ifndef ONLINE_JUDGE
68 fclose(stdin);
69 #endif
70 return 0;
71}