code for fun

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

随笔分类 -  数论/组合

摘要:所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余)。在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快、计算范围更大的算法,产生了快速幂取模算法。我们先从简单的例子入手:求abmodc算法1.直接设计这个算法:int ans = 1;for(int i = 1;i0) { if(b % 2 = = 1) ans = (ans * a) % c; b = b/2; a = (a * a) % c; } return ans;}本算法的时间复杂度为O(logb),能在几乎所有的程序设计... 阅读全文
posted @ 2014-03-01 01:45 xueda120 阅读(434) 评论(0) 推荐(0)

摘要:Problem DescriptionA number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n).InputThe input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 using 阅读全文
posted @ 2014-03-01 01:20 xueda120 阅读(179) 评论(0) 推荐(0)