↓ Skip to main content
  1. Posts/

BZOJ 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 (母函数,高精度)

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

1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店
#

Time Limit: 5 Sec Memory Limit: 64 MB Submit: 353 Solved: 190 [Submit][Status][Discuss]

Description
#

Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are: 1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1 Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).

约翰到奶牛商场里买工具.商场里有K(1≤K≤100).种工具,价格分别为1,2,…,K美元.约翰手里有N(1≤N≤1000)美元,必须花完.那他有多少种购买的组合呢?

Input
#

A single line with two space-separated integers: N and K.

仅一行,输入N,K.

Output
#

A single line with a single integer that is the number of unique ways FJ can spend his money.

不同的购买组合数.

Sample Input
#

5 3

Sample Output
#

5

思路:母函数裸题,还卡个高精度。。差评。。。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年04月14日 星期四 16时42分31秒
  4File Name :code/bzoj/1655.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;
 34const int MAXN =300;
 35int n,m;
 36int a[N][105],tmp[N][105];
 37
 38void add(int x,int y)
 39{
 40    for ( int i = 1 ; i <= 100 ; i++)
 41	tmp[x][i]+=a[y][i];
 42
 43    for ( int i = 1 ; i <= 100 ; i++)
 44    {
 45	tmp[x][i+1]+=tmp[x][i]/10;
 46	tmp[x][i]%=10;
 47    }
 48
 49}
 50
 51void tran( int x)
 52{
 53    for ( int i = 1 ; i <= 100 ; i++)
 54    {
 55	a[x][i] = tmp[x][i];
 56	tmp[x][i] =  0;
 57    }
 58}
 59
 60int main()
 61{
 62	#ifndef  ONLINE_JUDGE
 63	freopen("code/in.txt","r",stdin);
 64  #endif
 65
 66	scanf("%d %d",&n,&m);
 67	ms(a,0);
 68	m = min(m,n);
 69	for ( int i = 0 ; i <= n ; i++)
 70	{
 71	    a[i][1] = 1;
 72	    tmp[i][1] =  0 ;
 73	}
 74
 75	for ( int i = 2 ; i <= m ; i++)
 76	{
 77	    for ( int j = 0 ;j <= n ; j++)
 78	    {
 79		for ( int k = 0 ; k+j<= n ; k+=i)
 80		{
 81		    for ( int z = 1 ; z <= 40 ; z++)
 82		    {
 83			tmp[k+j][z] += a[j][z];
 84		    }
 85
 86		}
 87	    }
 88//	    cout<<"aaa"<<endl;
 89
 90	    for ( int j = 0 ; j <= n ; j++)
 91	    {
 92		for ( int z = 1 ; z <= 40 ; z++)
 93		{
 94		    tmp[j][z+1]+=tmp[j][z]/10;
 95		    tmp[j][z]%=10;
 96		}
 97	    }
 98	    for ( int j = 0 ; j <= n ; j++)
 99	    {
100		for ( int z = 1 ; z <= 40 ; z++)
101		{
102		    a[j][z] = tmp[j][z];
103		    tmp[j][z]  = 0;
104		}
105	    }
106	}
107
108	int len = 100;
109	while (a[n][len]==0) len--;
110//	cout<<"len:"<<len<<endl;
111	for ( int i = len  ;i >0 ; i--) printf("%d",a[n][i]);
112
113
114  #ifndef ONLINE_JUDGE
115  fclose(stdin);
116  #endif
117    return 0;
118}

Related

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

·698 words·2 mins
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)是不同的方法。

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奇偶性相同。