HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简

http://acm.hdu.edu.cn/showproblem.php?pid=4565

首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数)。

因为(a - 1)^2 < b < (a ^ 2),所以其不是完全平方数,假如是,那么设其为c,则有a - 1 < c < a,这是矛盾的

所以,向上取整这个步骤,是必不可少的了。

那么,我在它后面加上一个< 1的数,同时使得它们结合成为整数,那就相当于帮它取整了。根据二项式定理

(a + sqrt(b)) ^ n + (a - sqrt(b)) ^ n,其中的奇数次幂,都抵消了。所以这个是一个整数,而且(a - sqrt(b)) ^ n也是小于1的。刚好符合我们的要求。

所以Sn = (a + sqrt(b)) ^ n + (a - sqrt(b)) ^ n

现在就是要找Sn和S(n +1)的关系那些。

化简的时候,整体化简,

x = a + sqrt(b)

y = a - sqrt(b)

x + y = 2 * a

x * y = a * a - b

那么Sn = x^n + y^n  = (x + y) * (x^(n - 1) + y^(n - 1)) - (x * y) * (x ^ (n - 2) + y ^ (n - 2))

就是Sn = (x + y) * S(n - 1) - (x * y) * (S(n - 2))

然后矩阵快速幂

过程中要不断取模,防止中途溢出。

跪了。这题真的跪了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
LL a, b, n, m;
const int maxn = 4;
struct Matrix {
    LL a[maxn][maxn];
    int row;
    int col;
};
struct Matrix matrix_mul  (struct Matrix a, struct Matrix b, int MOD) {  //求解矩阵a*b%MOD
    struct Matrix c = {0};  //这个要多次用到,栈分配问题,maxn不能开太大,
    //LL的时候更加是,空间是maxn*maxn的,这样时间用得很多,4和5相差300ms
    c.row = a.row; //行等于第一个矩阵的行
    c.col = b.col; //列等于第二个矩阵的列
    for (int i = 1; i <= a.row; i++) { //枚举第一个矩阵的行
        for (int j = 1; j <= b.col; j++) { //枚举第二个矩阵的列,其实和上面数值一样
            for (int k = 1; k <= b.row; k++) { //b中的一列中,有“行”个元素 notice
                c.a[i][j] += a.a[i][k] * b.a[k][j];
                c.a[i][j] %= MOD;
            }
            c.a[i][j] = (c.a[i][j] + MOD) % MOD; //如果怕出现了负数取模的话。可以这样做
        }
    }
    return c;
}
struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, int n, int MOD) {
//求解a*b^n%MOD
    while (n) {
        if (n & 1) {
            ans = matrix_mul(ans, base, MOD);//传数组不能乱传,不满足交换律
        }
        n >>= 1;
        base = matrix_mul(base, base, MOD);
    }
    return ans;
}

void work() {
    if (n == 1) {
        cout << 2 * a % m << endl;
        return;
    }
    if (n == 2) {
        cout << (2 * a * a + 2 * b) % m << endl;
        return;
    }
    Matrix ma_a = {0};
    ma_a.row = 1, ma_a.col = 2;
    ma_a.a[1][1] = 2 * a * a + 2 * b, ma_a.a[1][2] = 2 * a;

    Matrix ma_b = {0};
    ma_b.row = 2, ma_b.col = 2;
    ma_b.a[1][1] = 2 * a, ma_b.a[1][2] = 1;
    ma_b.a[2][1] = -(a * a - b), ma_b.a[2][2] = 0;

    Matrix ans = quick_matrix_pow(ma_a, ma_b, n - 2, m);
    cout << ans.a[1][1] << endl;
}
int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    IOS;
    while (cin >> a >> b >> n >> m) work();
    return 0;
}

时间: 2024-08-03 11:24:45

HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简的相关文章

HDU 4565 So Easy!

线性推,矩阵乘法+快速幂求通项. 传送门:点击打开链接 #include <cstdio> #include <cstring> #include <cmath> #include <iostream> using namespace std; #define LL long long struct Mat{ LL f[2][2]; }; LL MOD; Mat mul(Mat a,Mat b) { LL i,j,k; Mat c; memset(c.f,0

数学(矩阵乘法):HDU 4565 So Easy!

So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3804    Accepted Submission(s): 1251 Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce

HDU 4565 So Easy! 矩阵快速幂 + 共轭数

题意:中文题 So Easy 解题思路: 这题应该是 HDU 2256的原题 , 根据类似的结论可以推出 中间矩阵 ta  1 tb ta 原矩阵 1 1 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月17日 星期三 11时35分45秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #inc

hdu 4565 So Easy! (共轭构造+矩阵快速幂)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求余,所以用矩阵快速幂加速求解过程的时候,会产生误差,就很自然地想到了凑数,因为(a-1)^2<b<a^2,得出0<a-sqrt(b)<1,则无论n取多大,(a-sqrt(b))^n都是小于1的,(a-sqrt(b))^n 与 (a+sqrt(b))^n共轭,两者展开后会相互抵销,所以(

【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)

链接 :click here~~ 题意: A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn. You, a top coder, say: So easy! [解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下

矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Google Codejam Round 1A的C题. #include <bits/stdc++.h> typedef long long ll; const int N = 5; int a, b, n, mod; /* *矩阵快速幂处理线性递推关系f(n)=a1f(n-1)+a2f(n-2)+.

hdu 4565 So Easy! 矩阵

So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2350    Accepted Submission(s): 729 Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ce

HDU 4565 So Easy!(构造共轭式+矩阵)(好题)

题意: 已给a,b是正数, 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231. 求: 那是向上取整符号 思路: 注意到(a-1)2< b < a2 而且(a+sqrt(b))^n与其共轭式的和显然为整数,又注意到它的共轭式(a-sqrt(b))^n小于1(由于a,b大小关系) 所以即求Sn=(a+sqrt(b))^n + (a-sqrt(b))^n 再变形(易变形)递推Sn=2*aSn-1 + (b-a^2)*Sn-

HDU 4565 So Easy! 矩阵快速幂

设(a+sqrt(b))^n为(Xn + Yn*sqrt(b)),那么显然有(a+sqrt(b))^(n+1) 为 (a*Xn + b*Yn + (aYn+Xn)*sqrt(b)). 那么显然有(a+sqrt(b))的Xn,Yn可以表示为 : 然后又会发现,(a-sqrt(6))^n可以表示为: 那么会发现(a+sqrt(b))^n = (a+sqrt(b))^n + (a-sqrt(b))^n - (a-sqrt(b))^n = Xn+Yn*sqrt(b) +Xn-Yn*sqrt(b) -(a