欧拉计划(Project Euler)

欧拉计划(Project Euler)是一个解题网站,站内提供了一系列数学题供用户解答,解题的用户主要是对数学计算机编程感兴趣的成年人及学生。目前该站包含了250多道不同难度的数学题,每一题都可以通过计算机程序在1分钟内求出结果。该网站自2001年起定期增加新的题目,每题都有对应的讨论区,注册用户在正确提交了某题的答案后才能进入该题的讨论区。 站内根据完成题目的数量将用户分为6个级别,设立了6个排行榜,并用正多面体球体来表示不同的级别。另外还设有一个欧拉人(Eulerians)排行榜。只有最新题目的前20位解答者才可以上榜。   我建立的google code地址:https://code.google.com/p/my-project-euler-code/

阅读全文 »

ZOJ 1088

ZOJ 1088 System Overload Time Limit: 10 Seconds Memory Limit: 32768 KB Recently you must have experienced that when too many people use the BBS simultaneously, the net becomes very, very slow. To put an end to this problem, the Sysop has developed a contingency scheme for times of peak load to cut off net access for some buildings of the university in a systematic, totally fair manner. Our university buildings were enumerated randomly from 1 to n. XWB is number 1, CaoGuangBiao (CGB) Building is number 2, and so on in a purely random order. Then a number m would be picked at random, and BBS access would first be cut off in building 1 (clearly the fairest starting point) and then in every mth building after that, wrapping around to 1 after n, and ignoring buildings already cut off. For example, if n=17 and m=5, net access would be cut off to the buildings in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off CGB Building last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that building 2 is the last building selected. Your job is to write a program that will read in a number of buildings n and then determine the smallest integer m that will ensure that our CGB Building can surf the net while the rest of the university is cut off. Input Specification The input file will contain one or more lines, each line containing one integer n with 3 Input is terminated by a value of zero (0) for n. Output Specification For each line of the input, print one line containing the integer m fulfilling the requirement specified above. Sample Input 3 4 5 6 7 8 9 10 11 12 0 Sample Output 2 5 2 4 3 11 2 3 8 16 代码如下: import sys test =[ 2 , 5 , 2 , 4 , 3 , 11 , 2 , 3 , 8 , 16 , 4 , 21 , 6 , 5 , 2 , 11 , 20 , 34 , 8 , 15 , 10 , 7 , 13 , 11 , 13 , 45 , 18 , 23 , 8 , 3 , 2 , 25 , 75 , 42 , 13 , 5 , 23 , 13 , 50 , 16 , 18 , 89 , 38 , 8 , 39 , 30 , 29 , 38 , 7 , 45 , 23 , 137 , 46 , 63 , 17 , 48 , 5 , 46 , 34 , 140 , 33 , 39 , 2 , 28 , 29 , 79 , 33 , 48 , 3 , 10 , 46 , 120 , 6 , 37 , 17 , 8 , 44 , 15 , 160 , 20 , 35 , 144 , 104 , 179 , 153 , 24 , 8 , 265 , 19 , 9 , 62 , 7 , 139 , 19 , 44 , 93 , 182 , 27 , 158 , 185 , 193 , 17 , 82 , 3 , 11 , 43 , 55 , 21 , 41 , 146 , 29 , 80 , 59 , 8 , 29 , 66 , 19 , 160 , 59 , 28 , 129 , 127 , 120 , 72 , 45 , 157 , 2 , 63 , 127 , 81 , 318 , 513 , 98 , 28 , 32 , 231 , 236 , 411 , 26 , 45 , 5 , 303 , 228 , 66 , 9 , 205 , 65 , 39 ] def check ( m , n ): r = 0 for i in range ( 2 , n ): r =( r + m )% i if r == 0 : return 1 else : return 0 if __name__ == '__main__' : for line in sys . stdin : a = int ( line ) if a != 0 : print test [ a - 3 ] ...

阅读全文 »

基本遗传算法(GA)

1、基本遗传算法描述 遗传算法在自然与社会现象模拟、工程计算等方面得到了广泛的应用。在各个不同的应用领域,为了取得更好的结果,人们对GA进行了大量的改进,为了不至于混淆,我们把Holland提出的算法称为基本遗传算法,简称 GA、SGA(Simple Genetic Algorithm )、CGA(Canonical Genetic Algorithm),将其它的 GA类 算法称为GAs(Genetic Algorithms),可以把GA看作是GAs的一种特例。 1.1、 基本遗传算法的构成要素 染色体编码方法 基本遗传算法使用固定长度的二进制符号串来表示群体中的个体,其等位基因由二值符号集{0,1}组成。初始群体中各个个体的基因值用均匀分布的随机数来生成。如:x;100111001000101101就可表示一个个体,该个体的染色体长度是 l=18。 个体适应度评价 基本遗传算法按与个体适应度成正比的概率来决定当前群体中每个个体遗传到下一代群体中的机会多少。为正确计算这个概率,这里要求所有个体的适应 度必须为正数或零。这样,根据不同种类的问题,必须预先确定好由目标函数值到个体适应度之间的转换规则,特别是要预先确定好当目标函数值为负数时的处理方法。 遗传算子 基本遗传算法使用下述三种遗传算子: 选择运算:使用比例选择算子; 交叉运算:使用单点交叉算子; 变异运算:使用基本位变异算子。 基本遗传算法的运行参数 基本遗传算法有下述4个运行参数需要提前设定: M:群体大小,即群体中所含个体的数量,一般取为20 ~ 100。 T:遗传运算的终止进化代数,一般取为100 ~ 500 pc:交叉概率,一般取为0.4 ~ 0.99 pm:变异概率,一般取为 0.0001 ~ 0.1 这4个运行参数对遗传算法的求解结果和求解效率都有一定的影响,但目前尚无合理选择它们的理论依据。在遗传算法的实际应用中,往往需要经过多次试算后才能确定出这些参数合理的取值大小或取值范围。 1.2、基本遗传算法的形式化定义 基本遗传算法可定义为一个7元组: GA= (M, F, s, c, m, pc, pm ) M 群体大小; F 个体适应度评价函数; s 选择操作算于; c 交叉操作算子: m 变异操作算于; pc 交叉概率; pm 变异概率; 1.3、基本遗传算法描述 Procedure GA Begin initialize P(0); t=0; while (t =T) do for i=1 to M do Evaluate fitness of P(t); end for for i=1 to M do Select operation to P(t); end for for i=1 to M/2 do Crossover operation to P(t); end for for i=1 to M do Mutation operation to P(t); end for for i=1 to M do P(t+1) = P(t); end for t=t+1 end while end ...

阅读全文 »