light oj 1052 String Growth

本文探讨了一个计算机科学领域的有趣问题——字符串增长。通过一系列数学运算和编程实现,文章详细解释了如何从初始字符串出发,经过多次转换后计算特定步骤下字符串的长度。
1052 - String Growth
Time Limit: 2 second(s)Memory Limit: 32 MB

Zibon just started his courses in Computer science. After having some lectures on programming courses he fell in love with strings. He started to play with strings and experiments on them. One day he started a string of arbitrary (of course positive) length consisting of only {a, b}. He considered it as 1st string and generated subsequent strings from it by replacing all the b's with ab and all the a's with b. For example, if he ith string is abab(i+1)th string will be b(ab)b(ab) = babbab. He found that the Nth string has length X and Mth string has length Y. He wondered what will be length of the Kth string. Can you help him?

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case begins with five integers N, X, M, Y, K. (0 < N, M, X, Y, K < 109 and N ≠ M).

Output

For each case print one line containing the case number and L which is the desired length (mod 1000000007) or the string "Impossible" if it's not possible.

Sample Input

Output for Sample Input

2

3 16 5 42 6

5 1 6 10 9

Case 1: 68

Case 2: Impossible

 


PROBLEM SETTER: MD. TOWHIDUL ISLAM TALUKDER
SPECIAL THANKS: JANE ALAM JAN

题意:一个字符串,每次a变成b,b变成ab,第一个ab的数量不确定,现在给你第n次m次变换后的结果的字符串的长度,求第k次变换后字符串的长度(mod1e9+7)
题意没说清楚,第n和m次的长度应该都是没mod过1e9+7的,不然这题就复杂了
假设第一个里面a的数量为x,b的数量为y,变换一次后变为y,x+y,总长度为x+2y,变换两次为x+y,x+2y,总长度为2x+3y,第三次变换后为x+2y,2x+3y,总长度为3x+5y,可以发现x和y的系数是按斐波拉契数列变化的,解方程判断是否能求出x,y对应的系数即可,x y也不能为负

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
const int mod=1e9+7;
ll p[3][3],ans[3][3];
int m,n,x,y,k,t,XX,Xx,xX,YY,Yy,yY;//定义成x1,y1什么的CE了,很迷

void multi(ll a[][3],ll b[][3])
{
    ll tmp[3][3];
    mem(tmp,0);
    for1(i,2)
        for1(j,2)
            for1(k,2)
                tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j])%mod;
    for1(i,2)
        for1(j,2)
            a[i][j]=tmp[i][j];
}

void init()
{
    mem(ans,0);
    mem(p,0);
    ans[1][1]=ans[2][2]=1;
    p[1][1]=p[1][2]=p[2][1]=1,p[2][2]=0;
}

void solve(int n,int& x,int& y)//矩阵快速幂计算x y的系数
{
    n--;
    init();
    while(n)
    {
        if(n&1)multi(ans,p);
        n>>=1;
        multi(p,p);
    }
    x=(ans[2][1]+ans[2][2])%mod;
    y=(ans[1][1]+ans[1][2])%mod;
}

int main()
{
    sf("%d",&t);
    for1(i,t)
    {
        init();
        int tag=0;
        sf("%d%d%d%d%d",&x,&n,&y,&m,&k);
        pf("Case %d: ",i);
        solve(x,XX,YY);
        solve(y,Xx,Yy);
        solve(k,xX,yY);
        if((XX*m-Xx*n)%(Yy*XX-YY*Xx))//不能整除,说明不能求出满足的x y,因为 m n没有mod过1e9+7,在x y大于0时要求出解,否则不满足
            puts("Impossible");
        else
        {
            ll YYY=(XX*m-Xx*n)/(Yy*XX-YY*Xx);
            if((n-YY*YYY)%XX)
                puts("Impossible");
            else
            {
                int XXX=(n-YY*YYY)/XX;
                if(XXX<0||YYY<0)
                {
                    puts("Impossible");
                }
                else
                {
                    ll ans=((ll)XXX*xX%mod+(ll)YYY*yY%mod)%mod;
                    ans=(ans+mod)%mod;
                    pf("%lld\n",ans);
                }
            }
        }
    }
    return 0;
}


### 矩阵特征值与斐波那契数列的关系 斐波那契数列可以表示为一系列整数序列,其中每一项都是前两项之和。为了理解矩阵特征值如何应用于斐波那契数列的计算,可以从差分方程的角度出发。 考虑斐波那契数列的定义: \[ F_{n+1} = F_n + F_{n-1} \] 这可以通过引入向量 \( U_n = \begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} \) 来转换成矩阵形式[^4]。具体来说, \[ U_{n+1} = \begin{bmatrix} F_{n+2} \\ F_{n+1} \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} = A U_n, \] 这里, \[ A = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}. \] #### 特征值与特征向量的应用 对于上述转移矩阵 \( A \),其特征多项式由下述行列式给出: \[ |A-\lambda I|= \left| \begin{array}{cc} 1-\lambda & 1\\ 1 & -\lambda \end{array}\right|=0. \] 解此二次方程可得两个根即为该矩阵的特征值 λ₁ 和 λ₂ 。这两个特征值分别是黄金比例 φ 及其共轭 ψ 的相反数[^1]: \[ λ_1=\frac{(1+\sqrt{5})}{2},\quad λ_2=\frac{(1-\sqrt{5})}{2}. \] 这些特征值允许我们将原始递推关系转化为更简单的指数增长模式。当初始条件给定时,利用对应的特征向量组合,可以直接写出通项公式来表达任意位置上的斐波那契数值。 #### 使用Python实现快速求解斐波那契数列 下面是一个基于以上原理编写的 Python 函数用于高效地获取大范围内的斐波那契数: ```python import numpy as np def fibonacci_matrix(n): if n == 0: return 0 elif n == 1: return 1 # 定义变换矩阵及其幂次运算 T = np.array([[1, 1], [1, 0]], dtype='float') result = np.linalg.matrix_power(T, n-1).dot(np.array([1, 0])) return int(result[0]) print(fibonacci_matrix(10)) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值