↓ Skip to main content
  1. Posts/

BZOJ 1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪(母函数)

·698 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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}

Related

BZOJ 1630/2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 (母函数)

·1569 words·4 mins
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 # Time Limit: 4 Sec Memory Limit: 64 MB Submit: 149 Solved: 85 [Submit][Status][Discuss] Description # 有一天,贝茜无聊地坐在蚂蚁洞前看蚂蚁们进进出出地搬运食物。很快贝茜发现有些蚂蚁长得几乎一模一样,于是她认为那些蚂蚁是兄弟,也就是说它们是同一个家族里的成员。她也发现整个蚂蚁群里有时只有一只出来觅食,有时是几只,有时干脆整个蚁群一起出来。这样一来,蚂蚁们出行觅食时的组队方案就有很多种。作为一头有数学头脑的奶牛,贝茜注意到整个蚂蚁群由T(1≤T≤1000)个家族组成,她将这些家族按1到T依次编号。编号为i的家族里有Ni(1≤Ni≤100)只蚂蚁。同一个家族里的蚂蚁可以认为是完全相同的。 如果一共有S,S+1,…,B(1≤S≤B≤A)只蚂蚁一起出去觅食,它们一共能组成多少种不同的队伍呢?注意:只要两支队伍中所包含某个家族的蚂蚁数不同,我们就认为这两支队伍不同。由于贝茜无法分辨出同一家族的蚂蚁,所以当两支队伍中所包含的所有家族的蚂蚁数都相同时,即使有某个家族换了几只蚂蚁出来,贝茜也会因为看不出不同而把它们认为是同一支队伍。 比如说,有个由3个家族组成的蚂蚁群里一共有5只蚂蚁,它们所属的家族分别为1,1,2,2,3。于是出去觅食时它们有以下几种组队方案: ·1只蚂蚁出去有三种组合:(1)(2)(3)

指数型母函数总结

·425 words·1 min
指数型母函数网上的资料不是很多,推荐毛杰明的09年国家集训队论文《母函数的性质及应用》 以及Richard A.Brualdi 所著的《组合数学》的第七章来看…倒不用全看懂..但是这本上面干货比较多。

poj 1322 chocolate (指数型母函数 )

·1136 words·3 mins
http://poj.org/problem?id=1322 题意: 思路:别看n,m很大,但是想一下,m显然不可能大于c(如果大于c,那么根据抽屉原理,至少存在一种巧克力大于一个,然而大于一个就会被取走…矛盾),这样概率为0。m也不可能大于n,因为最好的情况就是取出的巧克力都放在了桌子上,如果总共取的还不到n个,又怎么可能剩下m(m>n)个呢。此外,还需要n,m奇偶性相同,否则设n-m=2K+1,说明如果要剩余m个,那么就要减少2k+1个,但是巧克力是两个两个减少的,减少的个数一定是偶数,因此矛盾。所以n,m奇偶性相同。

poj 3734 Blocks (指数型母函数,泰勒级数展开)

·234 words·1 min
http://poj.org/problem?id=3734 题意+思路同******hdu2065红色病毒解题报告 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月27日 星期六 16时39分53秒 4File Name :code/poj/3734.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 LL MOD =1E4+7; 34LL n; 35LL res; 36LL ksm(LL a,LL b) 37{ 38 LL res = 1; 39 while (b) 40 { 41 if (b&1) res=(res*a)%MOD; 42 b = b>>1; 43 a = (a*a)%MOD; 44 } 45 return res; 46} 47int main() 48{ 49 #ifndef ONLINE_JUDGE 50 freopen("code/in.txt","r",stdin); 51 #endif 52 53 54 int T; 55 cin>>T; 56 while (T--) 57 { 58 scanf("%lld",&n); 59 60 LL ans = ksm(4,n-1)+ksm(2,n-1); 61 ans %=MOD; 62 printf("%lld\n",ans); 63 64 } 65 66 67 68 #ifndef ONLINE_JUDGE 69 fclose(stdin); 70 #endif 71 return 0; 72}