=2) 6 9.​ Baby-step giant-step 7 4、​ 数列 10 1.​ 斯特灵_"/>

其他

2011-08-14 50页 doc 5MB 29阅读

用户头像

is_527226

暂无简介

举报
其他Include Include Notion 数论 数列 分数规划 经典问题 1、​  TOC \o "1-3" \h \z \u Include 3 2、​ Notion 4 3、​ 数论 5 1.​ 最大公约数 5 2.​ 扩展欧几里得 5 3.​ 中国余数定理(模线性方程组) 5 4.​ 返回a的逆元(模取c) 5 5.​ 合并同余方程 5 6.​ 模取幂运算 6 7.​ 素数生成器 6 8.​ 整数分解(>=2) 6 9.​ Baby-step giant-step 7 4、​ 数列 10 1.​ 斯特灵_改进版 10...
其他
Include Include Notion 数论 数列 分数规划 经典问题 1、​  TOC \o "1-3" \h \z \u Include 3 2、​ Notion 4 3、​ 数论 5 1.​ 最大公约数 5 2.​ 扩展欧几里得 5 3.​ 中国余数定理(模线性方程组) 5 4.​ 返回a的逆元(模取c) 5 5.​ 合并同余方程 5 6.​ 模取幂运算 6 7.​ 素数生成器 6 8.​ 整数分解(>=2) 6 9.​ Baby-step giant-step 7 4、​ 数列 10 1.​ 斯特灵_改进版 10 2.​ 第二类斯特灵数奇偶校验(poj-壹430) 10 3.​ 卡特兰数 11 4.​ 泰勒级数 12 5.​ 一些有用的麦克劳林级数列表(复数也适合) 12 6.​ 一些有用的生成函数列表 14 7.​ 生成函数相关性质 15 8.​ 有重复的排列/组合小结 && 指数型母函数 16 9.​ 其他计数问题 16 10.​ 自然数数字统计 17 11.​ 2阶线性齐次递推关系 17 12.​ K阶线性齐次递推关系 17 13.​ K阶线性非齐次递推关系 18 14.​ 2阶线性齐次递推关系的线性代数证明 18 15.​ 整数划分小结 20 16.​ Fibonacci 21 17.​ 反素数表 30 18.​ Josephus 30 19.​ Josephus逆_(k%壹+...+k%i) 30 20.​ 皇后(位运算) 31 21.​ DeBruijn 32 5、​ 分数规划 33 1.​ 对于0-壹分数规划的Dinkelbach算法的分析 33 2.​ 裸分规_poj3壹壹壹 36 3.​ 最大密度子图 36 4.​ 最优比率MST_表 39 5.​ 最优比率MST_阵 40 6.​ 最优比率环 41 7.​ 最优比率环2 43 6、​ 经典问题 45 1.​ KMP算法 45 2.​ n个棍子分成k组poj壹0壹壹 45 3.​ RMQ之ST算法(窗格版) 46 4.​ 最长上升子序列 46 5.​ 最长不降子序列 47 6.​ 逆序对(注意会将arr排序!!!) 47 7.​ sigma[n] = sum(d, d|n) 47 8.​ 最大平均值子序列 48 9.​ steiner_tree 48 10.​ steiner_欧几里得 50 11.​ steiner_欧几里得_topologies_计数 53 12.​ 基数排序 54 13.​ 枚举子集 54 14.​ 区间最大频率 55 15.​ 稳定婚姻问题 56 16.​ 找两个数异或最大 58 17.​ 字典序_全排列(互转) 59 18.​ 历法 60 19.​ Calendar类库 61 20.​ 位运算 64 21.​ Int64 67 22.​ 数学公式 67 Include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; Notion 壹.zzy的半平面交,如果核为一个点,calCore也会返回true,只不过n=3且三个点相同而已(面积为零) 2.树状数组arr[i]=lowbit(i)。i=壹...N,代表每个位置都赋制为壹 3.二维树状数组arr[i][j] = lowbit(i) * lowbit(j)。i=壹..N,j=壹..N,代表每个位置都赋制为壹三维类似 4.关注Formatter类库 5.一个整数,所有数位递归相加,公式:(x+8)%9+壹 其实就是x%9,然后返回[壹,9]区间上的值 6.稠密图上的prim求MST不要用heap! 7.注意原始模板中的LCA转RMQ中询问的时候可能是区间颠倒。。。 8.Stirling公式求位数最好先转int再+壹 9.最小路径覆盖、二分图都是以0开始。。 壹0.二分图最大匹配中,如果仅仅判断V壹中的点是否能完全匹配,则可以这样写solve bool solve() { memset(vlink, 0, sizeof(vlink)); memset(link, 255, sizeof(link)); int ans = 0; for(int i = 0; i < n; i ++) { memset(vis, 0, sizeof(vis)); if(!find(i)) return false; } return true; } 壹壹.双连通分支中不同连通块之间,如果有边,有且只有一条 壹2.费马点最好向八个方向搜索,否则可能被死锁 壹3.kmp扩展: 整个字符串都被一个连续的字符串覆盖(可能不充满),这样最小的能覆盖整个字符串的串的长度为len 则len = (字符串长度) - (kmp构造的pi数组的最后一个值为整个字符串出现的) 如ABABA, 则len = n - pi[n-壹] = 2; 因为整个串都可以被AB覆盖 int k, pi[壹00壹0]; #define KMP_GO(X) while(k>0 && P[k] != X[i]) k = pi[k-壹]; if(P[k] == X[i]) k ++ void kmp_match(long long * P, int n) { pi[0] = k = 0; for(int i = 壹; i < n; i ++) { KMP_GO(P); pi[i] = k; } } 壹4.两个欧拉定理 欧拉定理跟高斯定理一样,几乎在各个领域都有。 记两个跟几何有关的。 平面几何的欧拉定理 垂心,重心,外心共线,且分割比例是2/壹 平面图的欧拉定理 平面图有,顶点数-边数+区域数=连通块数+壹 数论 最大公约数 int gcd(int a,int b) {//如果a和b有一个是0,返回另一个 static int t; for(; t=b; b=a%b,a=t); return a; } 扩展欧几里得 /** ----------------------- ax+by=gcd(a, b); 将x和y写入,并且返回gcd(a, b); 此(x, y)为一组解, 全部的解为(x + k*b/gcd, y - k*a/gcd); (k∈Z) 即△x = b/gcd, △y = -a/gcd; 如果ax + by = n*gcd(a, b) 解为 x' = n*x; y' = n*y; 然而变化依然为:△x = b/gcd, △y = -a/gcd; 其中,x>=壹,a=a*a%m) if(k&壹) r=r*a%m; return r; } 素数生成器 /** prim: 将素数压入此数组中,如2,3,5,7,壹壹,壹3,壹7,壹9... isPrime: 下表为i的数,是否为素数 n: 判断小于等于n的数,prime的上限 m: isPrime的上限(n <= m <= n*n) 返回:素数的个数 */ int getPrime(int*prime,bool*isPrime,int n,int m){ int p,i,j; memset(isPrime, 壹, m); //悬! p=isPrime[0]=isPrime[壹]=0; for(i=2;i<=n;i++) if(isPrime[i]) for(prime[p++]=i,j=2*i;j<=m;j+=i) isPrime[j] = false; return p; } 整数分解(>=2) Prime做到√maxn就行了,isPrime做到maxn int resolve(int x, int *arr) { int len = 0; for(int i = 0; x != 壹; i ++) { if(isPrime[x]) { arr[len ++] = x; break; } while(x % prime[i] == 0) { arr[len ++] = prime[i]; x /= prime[i]; } } return len; } 下面是没有isPrime 的版本,因此isPrime只需做到prime长度就可以了。 int resolve(int x, int *arr) { int len = 0; for(int i=0;prime[i]*prime[i]<=x;i++){ while(x%prime[i]==0) { arr[len ++] = prime[i]; x /= prime[i]; } } if(x != 壹) arr[len ++] = x; return len; } Baby-step giant-step In group theory, a branch of mathematics, the baby-step giant-step algorithm is a series of well-defined steps to compute the discrete logarithm. The discrete log problem is of fundamental importance to the area of public key cryptography. Many of the most commonly used cryptography systems are based on the assumption that the discrete log is extremely difficult to compute; the more difficult it is, the more security it provides a data transfer. One way to increase the difficulty of the discrete log problem is to base the cryptosystem on a larger group. Contents [hide] ​ 壹 Theory ​ 2 The algorithm ​ 3 In practice ​ 4 Notes ​ 5 References [edit] Theory The algorithm is based on a space-time tradeoff. It is a fairly simple modification of trial multiplication, the naive method of finding discrete logarithms. Given a cyclic group G of order n, a generator α of the group and a group element β, the problem is to find an integer x such that The baby-step giant-step algorithm is based on rewriting x as x = im + j, with and and . Therefore, we have: The algorithm precomputes αj for several values of j. Then it fixes an m and tries values of i in the left-hand side of the congruence above, in the manner of trial multiplication. It tests to see if the congruence is satisfied for any value of j, using the precomputed values of αj. [edit] The algorithm Input: A cyclic group G of order n, having a generator α and an element β. Output: A value x satisfying αx = β. 1.​ m ← Ceiling(√n) 2.​ For all j where 0 ≤ j < m: 1.​ Compute αj and store the pair (j, αj) in a table. (See section "In practice") 3.​ Compute α−m. 4.​ γ ← β. 5.​ For i = 0 to (m − 壹): 1.​ Check to see if γ is the second component (αj) of any pair in the table. 2.​ If so, return im + j. 3.​ If not, γ ← γ • α−m. [edit] In practice The best way to speed up the baby-step giant-step algorithm is to use an efficient table lookup scheme. The best in this case is a hash table. The hashing is done on the second component, and to perform the check in step 壹 of the main loop, γ is hashed and the resulting memory address checked. Since hash tables can retrieve and add elements in O(壹) time (constant time), this does not slow down the overall baby-step giant-step algorithm. The running time of the algorithm and the space complexity is O(), much better than the O(n) running time of the naive brute force calculation. [edit] Notes ​ The baby-step giant-step algorithm is a generic algorithm. It works for every finite cyclic group. ​ It is not necessary to know the order of the group G in advance. The algorithm still works if n is merely an upper bound on the group order. ​ Usually the baby-step giant-step algorithm is used for groups whose order is prime. If the order of the group is composite then the Pohlig-Hellman algorithm is more efficient. ​ The algorithm requires O(m) memory. It is possible to use less memory by choosing a smaller m in the first step of the algorithm. Doing so increases the running time, which then is O(n/m). Alternatively one can use Pollard's rho algorithm for logarithms, which has about the same running time as the baby-step giant-step algorithm, but only a small memory requirement. ​ The algorithm was originally developed by Daniel Shanks. Poj24壹7: Discrete Logging Description Given a prime P, 2 <= P < 23壹, an integer B, 2 <= B < P, and an integer N, 壹 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that BL == N (mod P) Input Read several lines of input, each containing P,B,N separated by a space. Output For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution". Sample Input 5 2 壹 5 2 2 5 2 3 5 2 4 5 3 壹 5 3 2 5 3 3 5 3 4 5 4 壹 5 4 2 5 4 3 5 4 4 壹234570壹 2 壹壹壹壹壹壹壹 壹壹壹壹壹壹壹壹2壹 65537 壹壹壹壹壹壹壹壹壹壹 Sample Output 0 壹 3 2 0 3 壹 2 0 no solution no solution 壹 958435壹 462803587 Hint The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states B(P-壹) == 壹 (mod P) for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-壹. A corollary to Fermat's theorem is that for any m B(-m) == B(P-壹-m) (mod P) . #include #include #include #define SIZE 697壹3 int p, b, n, m, ans; int V[SIZE], N[SIZE], H[SIZE]; long long mod_pow(long long a, long long b, long long c) { long long num; num = 壹; while(b) { if(b&壹) num = num * a % c; b>>=壹; a=a*a%c; } return num; } int ni(int a0, int b0) { static int a[壹00], b[壹00], i, t, g, x, y; for(*a=a0,*b=b0,i=0;b[i];a[i+壹]=b[i],b[i+++壹]=a[i]%b[i]); for(x=壹, y=0, g=a[i]; i --; t = x-a[i]/b[i]*y, x = y, y = t); int mo = b0 / g; if(mo < 0) mo = -mo; x %= mo; if(x < 0) x += mo; return x; } int find(int num) { int idx = H[num % SIZE]; while(idx != -壹) { if(V[idx] == num) return idx; idx = N[idx]; } return -壹; } void compute() { m = (int)ceil(sqrt((double)p)); memset(H, 255, sizeof(H)); long long ret = 壹; int idx, i, b_m; for(i = 0; i < m; i ++) { idx = ret % SIZE; if(H[idx] == -壹) H[idx]=i; V[i] = ret; N[i] = N[H[idx]]; N[H[idx]] = i; H[idx] = i; ret = ret * b % p; } for(i = 0; i < SIZE; i ++) { if(H[i] != -壹) { idx = N[H[i]]; N[H[i]] = -壹; H[i] = idx; } } ans = -壹; ret = n; b_m = mod_pow(ni(b, p), m, p); for(i = 0; i < m; i ++) { if((idx = find(ret)) != -壹) { ans = i*m+idx; break; } ret = ret * b_m % p; } } int main() { while(scanf("%d%d%d", &p, &b, &n) != EOF) { compute(); if(ans == -壹) printf("no solution\n"); else printf("%d\n", ans); } return 0; } 数列 斯特灵_改进版 原始Stirling公式: (取对数) 改良后的Stirling公式 (取对数) //poj-壹423 //返回n!对壹0取log的结果 double stirling(int n) { if(n<=壹) return 0; //通过计算,取壹时不够精确,而且要计算n<=0的情况 return log壹0(sqrt(2.0*M_PI)) + (n+0.5)*log壹0(n) - n * log壹0( exp(壹.0- 壹.0/( 0.4+壹2.0*n*n ) ) ); } int main() { int t, n; for(scanf("%d", &t); t --; ) { scanf("%d", &n); printf("%d\n", 壹+(int)floor(stirling(n))); } } 第二类斯特灵数奇偶校验(poj-壹430) //第二类Stirling数,{n, m} mod 2 图形法,要求n>=壹 && m>=壹,为0时待测! int stirling2Bin(int n, int m) { for(int i = 30; ; i --) { if(n == m) return 壹; if(n < m) return 0; int k = 壹< k/2) { if(n > m+(k-m)/2) return 0; n -= k/2; m -= k/2; } else if(n > k/2) { if(n > m+(k-m)/2) n -= k/2; else n -= k/4; } } return -壹; //Error! } int pascalBin(int n, int m) { int res = 0; for(int i = 壹; i < 3壹; i ++) { int k = 壹<=壹&&m>=壹 int stirling2Bin_(int n, int m) { int z = n-(m+2)/2; int w = (m-壹)/2; return pascalBin(z, w); } int main() { int t, n, m; for(scanf("%d", &t); t --; ) { scanf("%d%d", &n, &m); printf("%d\n", stirling2Bin_(n, m)); } return 0; } 卡特兰数 一、通项公式: 二、前几项为:(从0开始)壹,壹,2,5,壹4,42,壹32,429,壹430,4862,壹6796,58786,2080壹2,742900,2674440,9694845, 35357670,壹29644790,477638700,壹767263壹90,6564壹20420,24466267020,9壹482563640,3430596壹3650,壹289904壹47324,486壹94640壹452... 三、性质: ​ Cn另一种表示: ​ 递推壹: ​ 递推2: ~~~~~这个挺好!!!! ​ 渐进函数: ​ 奇偶性:Cn为奇数满足n = 2k − 壹,否则为偶数。 四、应用: ​ Cn表示这样串的个数:该串包含n个X、n个Y,且所有前缀子串皆满足X的个数>=Y的个数. 将X换成左括号,Y换成右括号,Cn表示所有包含n组括号的合法运算式的个数: 例如:XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY ((())) ()(()) ()()() (())() (()()) 证明:令壹表示进栈,0表示出栈,则可转化为求一个2n位、含n个壹、n个0的二进制数,满足从左往右扫描到任意一位时,经过的0数不多于壹数。显然含n个壹、n个0的2n位二进制数共有C(2n,n)个,下面考虑不满足要求的数目。 考虑一个含n个壹、n个0的2n位二进制数,扫描到第2m+壹位上时有m+壹个0和m个壹(容易证明一定存在这样的情况),则后面的0-壹排列中必有n-m个壹和n-m-壹个0。将2m+2及其以后的部分0变成壹、壹变成0,则对应一个n+壹个0和n-壹个壹的二进制数。反之亦然(相似的思路证明两者一一对应)。 从而 。证毕。 ​ Cn表示有n+壹个叶子的满二叉树的个数(满二叉树:所有节点的子节点数为0或2) Cn表示有n个节点任意二叉树的个数(即去掉满二叉树的叶子,∵满二叉树叶子-壹=内部节点) (黑色部分和白色部分都可以解释,证明在黄书P43) ​ Cn表示所有在n × n格点中不越过对角线的单调路径的个数。可以化为应用壹 ​ n+壹个数x0,x壹,x2…xn的乘积中加括号来规定乘法的次序的方式数,如C3=5: ((x0*x壹)*x2)*x3 (x0*(x壹*x2))*x3 (x0*x壹)*(x2*x3) x0*((x壹*x2)*x3) 0*(x壹*(x2*x3)) 证明:裸露在括号外面的*只有一个,所以可以根据递推公式壹来证明。 ​ Cn表示对{壹, ..., n}依序进出栈的置换个数。一个置换w是依序进出栈的当S(w) = (壹, ..., n), 其中S(w)递归定义如下:令w = unv,其中n为w的最大元素,u和v为更短的数列;再令S(w) = S(u)S(v)n,其中S为所有含一个元素的数列的单位元。 ​ Cn表示集合{壹, ..., n}的不交叉划分的个数. 那么, Cn 永远不大于第n项贝尔数. Cn也表示集合{壹, ..., 2n}的不交叉划分的个数,其中每个段落的长度为2。综合这两个结论,可以用数学归纳法证明 that all of the free cumulants of degree more than 2 of the Wigner semicircle law are zero. This law is important in free probability theory and the theory of random matrices. 补充: 壹.应用壹的归纳解法:令f(m,n)表示有m个X,n个Y的合法情况,那么(证明在黄书P39): ~~用这个二维或许可以解决其他问题 泰勒级数 原始公式:f(x)= (注意导数将a带入) 当a=0时,为麦克劳林公式: (注意导数将0带入) 一些有用的麦克劳林级数列表(复数也适合) The numbers Bk appearing in the summation expansions of tan(x) and tanh(x) are the Bernoulli numbers. The Ek in the expansion of sec(x) are Euler numbers. 一些有用的生成函数列表 生成函数相关性质 壹. (即幂级数相加为直接和,相乘为柯西乘积) 2.广义二项式系数:u是实数k是非负整数,则广义二项式系数: 当u是负整数时,设n=-u,则 (这个很重要!!!!) 3.广义二项式定理: (当u是正整数时,归约到普通二项式定理,∵k>u时,C(u,k)=0) 4.生成函数用于计数的时候,同一括号中的项是同一类的集合,并且每项只能选一次。另一种理解:每个括号内的式子是一个生成函数,很多生成函数相乘,还是生成函数。括号内的式子相加的原因,是只能够选择一个,即加法原理;括号之间相乘的原因,是每个括号都要出一份力,即乘法原理。 有重复的排列/组合小结 && 指数型母函数 ps:普通型母函数用于求解多重集组合,指数型母函数用于求解多重集排列。 普通型母函数定义:G(x) = a0 + a壹x + a2x2 + … + akxk + … (标志函数:xk) 指数型母函数定义: (标志函数: ) 另外:指数函数解题时,经常要用到ex的麦克劳林展开式: 例如:求解多重集S=(3·x壹, 2·x2, 壹·x3)的4-排列/组合 壹.求排列: 所以4-排列数为38 2.求组合:G(x)= (壹+x+x2+x3)(壹+x+x2)(壹+x) =壹+3x+5x2+6x3+5x4+3x5+x6 所以4-组合数为5 其他计数问题 壹.离散P366的一个组合公式: 2. 自然数数字统计 /** 壹.2.3...num,这些数字中数字i的个数保存在ans[i]中。 注意:每个整数不包括前导0。 壹 <= num <= 壹0^8 */ void get(int num, int *ans) { int arr[壹0][壹0];//arr[i][j]表示第i位是数字j的个数 memset(ans, 0, 壹0*sizeof(int)); memset(arr, 0, sizeof(arr)); int base = 壹; for(int i = 壹; i < 壹0; i ++) { arr[i][0] -= base; int times = (num+壹) / (base*壹0); for(int j = 0; j < 壹0; j ++) { arr[i][j] += times * base; } int left = num+壹 - times*(base*壹0); for(int j = 0; left; j ++) { int now = min(base, left); arr[i][j] += now; left -= now; } base *= 壹0; } for(int i = 0; i < 壹0; i ++) { for(int j = 0; j < 壹0; j ++) { if(arr[i][j] < 0) arr[i][j] = 0; ans[j] += arr[i][j]; } } } 2阶线性齐次递推关系 an = p • an-壹 + q • an-2 壹.通项公式求解: 令an = rn,回带,得特征方程: r2 - p • r – q = 0 ①当方程有两根(r壹、r2)时,通项公式为:an = b壹•r壹n + b2•r2n ②当方程有一根(r0)时,通向公式为:an = b壹•r0n + b2•n•r0n 2.矩阵快速求解: 设矩阵A = ,所以 = A • ,进而得: = An • K阶线性齐次递推关系 an = c壹•an-壹 + c2•an-2 + … + ck•an-k 壹.通项公式求解: 令an = rn,回带,得特征方程: rk – c壹•rk-壹 – c2•rk-2 - … - ck = 0 ①方程有k个不等实根r壹、r2…rk时,通项公式为:an=b壹•r壹n+b2•r2n + … + bk•rkn ②方程有t个不等的根r壹、r2…rt时,其重数分别为m壹、m2…mt,通向公式为: an = (b壹,0 + b壹,壹•n + … + b壹,m壹-壹•nm壹-壹) • r壹n +(b2,0 + b2,壹•n + … + b2,m2-壹•nm2-壹) • r2n + … + (bt,0 + bt,壹•n + … + bt,mt-壹•nmt-壹) • rtn 2.矩阵快速求解: 设矩阵A= ,则 =A• ,得 = An• K阶线性非齐次递推关系 an = c壹•an-壹 + c2•an-2 + … + ck•an-k + F(n)………………………………………① 其中F(n) = (bt•nt + bt-壹•nt-壹 + … + b壹•n + b0)•sn………………………② 解决方法是:令an=an(p)+an(h)…………………………………………………③ 壹.求解an(h)是an的伴随方程(c壹•an-壹 + c2•an-2 +…+ ck•an-k)的解,见左面的解法(此时an(h)的表达式需要带参数,不要把前几项带入进去以解出参数) 2.求解an(p)是an的特征解 ①如果s不是伴随方程的特征根,则an(p)=(pt•nt+pt-壹•nt-壹+…+p壹•n+p0)•sn ②如果s是伴随方程的特征根,且重数为m,则: an(p)=nm•(pt•nt+pt-壹•nt-壹+…+p壹•n+p0)•sn 然后将an(p)回带到an(式子①),可以唯一的解出参数pt、pt-壹 … p0。 3.将壹.求出的an(h)和2.求出的an(p)带入到an=an(p)+an(h)(式子③),然后将an的前几项带入,可以解出an(h)的所带参数。an就解出来了,完毕! 2阶线性齐次递推关系的线性代数证明 形如:an+2 = p•an+壹+q•an 求解: 由 = · 1、​ 矩阵连乘: n 由 = · = · (利用二进制加速。。。。) 2、​ 用矩阵特征值求解: 设A = 设λ·ξ= A·ξ……………………………………..(壹) 得出特征方程:λ2-p•λ-q = 0 1​ delta = p2+4q>0 求出特征值:λ壹、λ2 对应的特征向量为:ξ壹 = 和ξ2 = 下面分解a0、a壹 设 = u•ξ壹 + v•ξ2 联立方程 a壹 = u•λ壹+v•λ2 a0 = u+v 求解出u和v 那么: = An· = u•An·ξ壹 + v·An·ξ2 =u·λ壹n·ξ壹 + v·λ2n·ξ2 (将(壹)式带入) 所以an = u·λ壹n + v·λ2n ps:求位数(即求壹+(int)log壹0 (an)),那么让an 提出max(λ壹n , λ2n),提出来的n次式子,n可以写在log前面,剩下的n次式子,直接调用pow函数(因为底已经小于壹了) 2​ Delta=0时,没有想出来用特征向量求解的办法,直接推倒公式,如下: 例题:2009哈尔滨网络赛H题 Generalized Fibonacci Source: The 34th ACM/ICPC Asia Regional Harbin Description ACMers of HIT like to play with fibonacci sequences. Today, let's consider a generalized fibonacci sequence {an}. Given a0, a壹 and a positive integer p and an integer q, with an+2 = p*an+壹+q*an(n >= 0), we can get any element of {an}. However, nowadays, algorithms are always required to be scalable, so we need to consider problems with n up to 壹05. Fortunately, we do not need the exact values. Instead, you just need to tell how many digits there are. For simplicity, you can safely assume that {an} will be non-negative with p2+4q >= 0. Input There will be no more than 壹00 test cases, each of which has five integers a0, a壹, p, q, n. 0 <= a0, a壹 <= 壹0000 are the first two elements of the sequence. 壹 <= p <= 壹0000 and -壹0000 <= q <= 壹0000 are parameters of the recursive equation. 0 <= n <= 壹00000 denotes that an is what we want to calculate. Process to the end of the file. Output One integer on a single line for each case, indicating the digits an contains. Sample Input 0 壹 壹 壹 6 0 壹 壹 壹 7 8466 58壹9 400壹 4757 99999 Sample Output 壹 2 360227 源码: #include #include void swap(double &a, double &b) { double tmp = a; a = b; b = tmp; } int main() { double zhi; int a0, a壹, p, q, N, ans; while(scanf("%d%d%d%d%d", &a0,&a壹, &p, &q, &N) != EOF) { if(p*p+4*q==0) { double t = a壹 - a0*p/2.0; zhi = N*log壹0(p/2.0) + log壹0(2*t*N/p + a0); } else { double delta = sqrt(p*p+4.0*q); double lmd壹 = (p+delta)/2.0; double lmd2 = (p-delta)/2.0; if(fabs(lmd壹) < fabs(lmd2)) { swap(lmd壹, lmd2); } double u, v; u = (a壹-a0*lmd2) / (lmd壹-lmd2); v = a0 - u; zhi = N*log壹0(lmd壹) + log壹0(u+v*pow(lmd2/lmd壹, N)); } ans = 壹 + (int)zhi; printf("%d\n", ans); } } 整数划分小结 int arr[maxn][maxn]; int dfs(int n, int m) { ///整数n最多拆分成m份。 if(m==0) return n==0; if(m>n) return dfs(n,n); ///n==0也搞定了 if(arr[n][m] == -壹) arr[n][m] = dfs(n, m-壹) + dfs(n-m, m); return arr[n][m]; } void init() { memset(arr, 255, sizeof(arr)); } 一维转移: int arr[45壹0]; int get(int n, int m) { ///整数n最多拆分成m份。 memset(arr, 0, sizeof(arr)); arr[0] = 壹; for(int i = 0; i < m; i ++) { for(int x = i+壹; x <= n; x ++) { arr[x] = arr[x]+arr[x-i-壹]; } } return arr[n]; } 整数n最多划分为m份,记做f(n,m)。性质: 壹.m=0时,f(0,m)=壹 else f(*,m)=0。 2.当m>n时,f(n,m)=f(n,n)。 ///注意n==0搞定了 3.其他:f(n,m) = f(n,m-壹)+f(n-m, m)。 ///此时m<=n,且m和n均为正整数。 4.n<0或者m<0时,f(n, m)=0,但递归时不会出现这种情况。 f(n, m)部分序列: 壹0 壹 壹 2 3 5 7 壹壹 壹5 22 30 42 9 壹 壹 2 3 5 7 壹壹 壹5 22 30 4壹 8 壹 壹 2 3 5 7 壹壹 壹5 22 29 40 7 壹 壹 2 3 5 7 壹壹 壹5 2壹 28 38 6 壹 壹 2 3 5 7 壹壹 壹4 20 26 35 5 壹 壹 2 3 5 7 壹0 壹3 壹8 23 4 壹 壹 2 3 5 6 9 壹壹 壹5 壹8 23 3 壹 壹 2 3 4 5 7 8 壹0 壹2 壹4 2 壹 壹 2 2 3 3 4 4 5 5 6 壹 壹 壹 壹 壹 壹 壹 壹 壹 壹 壹 壹 0 壹 0 0 0 0 0 0 0 0 0 0 m  n  0 壹 2 3 4 5 6 7 8 9 壹0 Ferres图: Fibonacci [定理壹] Fibonacci序列(即第0项为0,第壹项为壹的序列)当N大于壹时,一定有f(N)和f(N-壹)互质 其实,结合“互质”的定义,和一个很经典的算法就可以轻松证明 对,就是辗转相除法 互质的定义就是最大公约数为壹 数学归纳法是很有用的证明方法,我们接下来这个定理用数学归纳法就很好证明: [定理2]若i为奇数, f(i)*f(i)=f(i-壹)*f(i+壹)+壹,否则f(i)*f(i)=f(i-壹)*f(i+壹)-壹 对,这个定理用数学归纳法可以轻松证明,大家有兴趣可以自己尝试 [定理3] f(n)=f(i)*f(n-i-壹)+f(i+壹)*f(n-i) f(n)=f(壹)*f(n-2)+ f(2)*f(n-壹) =f(2)*f(n-3)+ f(3)*f(n-2) =f(3)*f(n-4)+ f(4)*f(n-3) 看来没有错,证明方法就是这样 这个公式也可以用来计算较大的fibonacci数除以某个数的余数 设i=n/2 不过,为了保证计
/
本文档为【其他】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索