代码改变世界

随笔分类 -  二分法

NYOJ 503 二分

2012-03-26 09:50 by javaspring, 168 阅读, 收藏,
摘要: 直接用二分求答案就可以了,,题目:解方程时间限制:1000ms | 内存限制:65535KB难度:3描述Now,given the equation 8*x^4 - 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;Now please try your lucky.输入The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T line 阅读全文

杭电 3306 Another kind of Fibonacci 构造矩阵二分幂

2012-03-23 17:19 by javaspring, 263 阅读, 收藏,
摘要: 这道题题意是比较简单的,方法也很容易想,构建矩阵,主要难度在构建矩阵的地方。由于A(n)=x*A(n-1)+Y*(n-2),因此可以把A(n)平方,再由S(n)=S(n-1)+A(n)*A(n),所以可以构建矩阵{A(n-2)*A(n-2),A(n-1)*A(n-1),A(n-1)*A(n-2),S(n-1)},{A(n-1)*A(n-1),A(n)*A(n),A(n-1)*A(n),S(n)},构建出矩阵之后就很容易了。。题目:Another kind of FibonacciTime Limit: 3000/1000 MS (Java/Others)Memory Limit: 655... 阅读全文

模板 n维矩阵的二分幂

2012-03-23 15:43 by javaspring, 198 阅读, 收藏,
摘要: 今天一上午一下午就干了点这,就写了一个这样恶心的模板,,写出来我自己都不想看,,不过还好,可以用。。这个模板可以求n维矩阵的二分幂,主要适用于递推式求值的题目。。代码:#include <iostream> #include <cstdio> #include <string.h> using namespace std; typedef long long ll; const ll MAX=1000007; const int row=3;//矩阵的维数,根据题目要求改变 ll aa[11][11],bb[11][11];//aa,bb存储矩阵 ll cc 阅读全文

杭电2842 Chinese Rings 构建矩阵二分幂

2012-02-23 08:21 by javaspring, 189 阅读, 收藏,
摘要: 由题意可以求得f(n)=2*f(n-2)+f(n-1)+1,之后构建矩阵用矩阵二分幂就可以解决了。。。。。。。题目:Chinese RingsTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 363Accepted Submission(s): 225Problem DescriptionDumbear likes to play the Chinese Rings (Baguenaudier). It’s a game played wi... 阅读全文

杭电 2852 树状数组+二分

2012-02-17 13:59 by javaspring, 182 阅读, 收藏,
摘要: 题目:KiKi's K-NumberTime Limit: 4000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1314Accepted Submission(s): 565Problem DescriptionFor the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar 阅读全文

杭电2855 Fibonacci Check-up

2012-02-15 08:45 by javaspring, 173 阅读, 收藏,
摘要: 是一道数论方面的题,融合了矩阵二分幂和多项式定理以及斐波那契数列的一道题。先用公式可以推出来,之后用矩阵二分幂就可以了。关于矩阵二分幂,实际上就是矩阵的乘法,由于矩阵满足结合律,故可以用平方来计算,就达到了log(n)的复杂度,由于n可能为奇数,可能为偶数,所以当n为奇数时,还需要乘以初始矩阵。ac代码:#include <iostream> #include <cmath> #include <stdio.h> using namespace std; int m; int matrix[2][2];//存储结果的矩阵 void cheng(int n){ 阅读全文

北邮12月份月赛 D 矩阵二分幂

2012-01-06 13:01 by javaspring, 163 阅读, 收藏,
摘要: 是一道数学题吧,用矩阵二分幂可以轻松解决。以前在NYOJ上做过类似的题,所以比赛时直接贴的以前的代码,,水过。。。。。数据太水啊。。。。。题目:D零零漆的跳槽Accept:42Submit:117Time Limit:1000MSMemory Limit:65536KBDescription世上没有铁饭碗, 也没有永远的老板,跳槽风也刮到了组织,尽管组织极力想挽留零零漆,但他还是依然要跳到另一个有关部门.凭他的经验以及高超的杀猪功力,他顺利的通过了有关部门前面的测试,来到了算法测试关.给他的问题很简单----给两个整数n,m,求斐波纳契数fib[n] % m...算卖肉钱久了,零零漆还真想.. 阅读全文

NYOJ 420 二分法

2011-09-28 18:56 by javaspring, 151 阅读, 收藏,
摘要: 二分法的应用,,,题目:一个很简单的问题,求1^p+2^p+3^p+……+n^p的和。输入第一行单独一个数字t表示测试数据组数。接下来会有t行数字,每行包括两个数字n,p,输入保证0<n<=1000,0<=p<=1000。输出输出1^p+2^p+3^p+……+n^p对10003取余的结果,每个结果单独占一行。样例输入2 10 1 10 2 样例输出55 385ac代码:#include <iostream> #include <string.h> #include <string> #include <cmath> #in 阅读全文

poj 2002 二分法的强大应用

2011-09-22 18:31 by javaspring, 209 阅读, 收藏,
摘要: 自认为是一道很有难度的题,,同时也再一次体会到了二分的强大,,,,题目:A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a. 阅读全文