NWERC 2005 Unequalled Consumption

以下内容引用自frkstyc’s Blog, 原文地址:
http://www.blog.edu.cn/user2/frkstyc/archives/2006/1180760.shtml

——————————————————-我是分隔线——————————————————

有人曾经在POJ webboard上声称生成函数是“没用的东西”。但这个题却是利用生成函数解决问题的一个很好的例子。

题目的大意是有不超过5种重量在10以内的糖果(不同的糖果可以有相同重量),给一个整数P<= 1015,要求一个最小的重量w,只得做一个重量为w的糖果盒的不同方法数至少是P

看到P的规模就可以立即否决普通的动态规划解法。容易联想到的算法是二分法,但这个题乍看之下没有明显的单调性。尝试用生成函数解决。为描述方便,以下直接用一个实例来说明。

假设有3种糖果,重量分别为2, 3, 5。用生成函数中xn项的系数表示做重量为n的糖果盒的方法数,那么对应的生成函数就是

f(x) = 1 / ((1 – x2)(1 – x3)(1 – x5))。

这个形式是没有办法直接利用的,因为里面三个因子的展开式都有无穷项,要直接找系数的通项公式也不容易。但是利用一个很简单的技巧就可以解决这两个问题。(应当说明,这个技巧虽然看起来很简单,但的确是要费一番思考的。)注意到2, 3, 5的最小公倍数[2, 3, 5] = 30,因此将上面的式子改写一下,变成

g(x) = ((1 – x30) / (1 – x2)) ((1 – x30) / (1 – x3)) ((1 – x30) / (1 – x5)),
h(x) = 1 / (1 – x30)3,
f(x) = g(xh(x)。

这样,g(x)是一个一般的多项式,h(x)虽然有无穷项但形式很规整,f(x)只是g(x)和h(x)的乘积。

g(x)只需直接展开即可,展开的结果是一个次数小于3 * 30 = 90次的多项式。对于h(x),为方便起见,作一个换元,记

h’(y) = 1 / (1 – y)3。

通过计算或者查阅工具书可以知道h’(y)中yn项的系数为组合数C(3 – 1, n + 3 – 1),其中的3就是h’(y)的解析式中1 – y的次数。因此h(x)中x30 n的次数也是C(3 – 1, n + 3 – 1),而其他项的系数都是0。

综合上面的讨论,可以得到计算f(x)中xn项的系数的方法。记p(x)中xn项的系数为c(pn)。对于n< 90,c(fn)可以直接利用动态规划或者限制次数地直接展开f(x)得到。(注意:这个不是c(g,n)!)对于n >= 90,记n = 30 k + r,其中0 <= r < 30,那么根据f(x) = g(xh(x),有以下公式

c(fn) = C(2, k + 2) c(gr) + C(2, k + 1) c(gr + 30) + C(2,  kc(gr + 60)。

f(x)直接相关的计算已经基本解决。剩下求最小的问题,方法还是二分法。虽然从整体看,c(f,n)关于n不一定是单调的,但是假如把n写成n = 90 k + r,其中0 <= r < 90,就可以发现对固定的r,c(f, 90 k + r)关于k是单调的。因此只需枚举r,对k应用二分法,然后从所有候选的n = 90k + r中选取最小的即可。

We failed

This afternoon we did a practice with yzf, but all the three team failed, sigh~

Problems from NordicCPC2005, one of my favorite regional, I typed the first 4 code, then helped moon to finished our 5th code, we kept silence in the middle, which makes us looks like a second-class team. We failed on the Problem B, a Min-Max game, which should be a part of my knowledge architectonic, but which makes me unacceptable is that yzf used a greedy algorithm to solved it.

We need a long way to go, to be a first-class team. We are the Moonmist~

A Problem for Others

Gardon asked me to produce a problem for his own contest a weed ago, which contest celebrate for his remove. I make the problem that can be solved by a dynamic programming, which is my favorite recently, and used half a hour to write the standard code and the program to generate data. But when I asked flymouse to check the data he said that I make the problem hard to understand, I explained a minutes then decide to give up, replaced to rewrite the problem by English, maybe English is more understandable.

Nothing

Suddenly I found there is nothing worth to be recorded, or I’m getting lazing. Homework of Java and other courses are needed to be finished, many things have to do, the project of SE must be start up this week… Actually there is too many things to do but I don’t like to.

使用C/C++工作的Eclipse的安装

最近突然很多人要用Eclipse, 不仅仅是写Java, 也要使用C/C++, 很多问题, 一起写出来吧 :)

实验室的配置是WindowsXP SP2 + Eclipse3.2 with CDT Embedded for Win32 + MinGW 3.0.0.1 + JDK 1.5.02, 本次讨论最重要的Eclipse3.2withCDT却没找到合适的下载, 官方给的没有集成版的, 只有自己去找一下,我有上传过到 药学院 的FTP, 可惜好像被删了. MinGW和JDK都可以在官方主页上找下载, 或者你可以点 MinGW 来从台湾的镜像下载MinGW, 访问 Java 2 SDK 来从yaguo下载JDK.

首先安装JDK, 假设你安装到了默认目录, 然后将bin目录加入到环境变量中. 然后安装MinGW, 将bin目录加入到环境变量中, 并且把bin目录下的mingw32-make.exe改名为make.exe. 最后解压Eclipse到任何你喜欢的地方, Eclipse是一个基于Java编写的绿色软件, 不用担心安装问题, 打开后配置好WorkSpace的位置, 就可以任意使用了. 关于Eclipse的具体使用方法, 这就是另一个话题了…

附: 关于环境变量, 在桌面上打开[我的电脑], 选[属性], 选[高级]标签页, 点开下面的[环境变量], 双击下面系统变量中的Path, 要加入的就是在原始的后面加;和要添加的目录了, 比如我的在装了JDK和MinGW以及gVIM后的环境变量为

%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32Wbem;C:Program FilesJavajdk1.5.0_02bin;C:MinGWbin;C:Program FilesVimvim70

Mid-Autumn day

The day for family to get-together and for a celebration in Chinese custom, I spent 2 hour to write a code for the practice contest of ACM/ICPC Asia Regional 2006, Beijing, Internet Preliminary Contest in the eve, but I feel happy because these things I did are what I’d like to do.

 Oct. 6th is also the birthday of my father, so I wanna say "Happy Birthday, My Lovely Daddy!" now, but it’s too late, so I’ll give a call tomorrow(or the next daytime?) and send my blessing to my dad, who is the most important person and the best teacher in my life.

Happy Mid-Autumn day! Wish everyone can have a nice moon at this night.

Houxian – The Ninth Princess

The ninth princess, the singer Houxuan’s second album, is worth to have a try, I like the voice softly, which can make me quite and comfortable. The singer Houxuan, who is not very famous and always be considered as Jay Chou(Zhou Jielun), is said as the first R&B singer in mainland, his first album Curio(GuWan) makes many people to know him, it’s great too.

I suggest you to try it, buy a CD or download a demo from the Internet, you’ll amazing it.

Minimal Sets of .vimrc

A 14 lines only .vimrc files for the vim under linux, I can use it everywhere as I used to be.

set nocompatible " get out of horrible vi-compatible mode
syntax on " syntax highlight on
set ruler " Always show current positions along the bottom
set number " trun on line number
set showmatch " show matching brackets
set ai " autoindent
set si " smartindent
set cindent " do c-style indenting
set tabstop=4 " tab spacing
set softtabstop=4 " tab spacing
set shiftwidth=4 " tab spacing
set expandtab " not real tabs please!
set smarttab " use tabs at the start of a line, spaces elsewhere
set so=5 " Keep 5 lines (top/bottom) for scope