So Easy!(HDU - 4565)
Problem Description
A sequence S n 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 S n.
You, a top coder, say: So easy!Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 2 15, (a-1) 2< b < a 2, 0 < b, n < 2 31.The input will finish with the end of file.
Output
For each the case, output an integer S n.
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013Sample Output
4
14
4
题意:给出 a、b、n、m 按照上图的公式,求 Sn
思路:共轭矩阵的构造,具体思路:点击这里
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=1e9+7;
const int N=10+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
struct Matrix{
LL s[N][N];
};
Matrix e;//单位矩阵E
Matrix x;//构造矩阵
LL mod;
void init(){
for(int i=1;i<=2;i++)//主对角线为1
e.s[i][i]=1;
}
Matrix mul(Matrix A,Matrix B,LL n){//矩阵乘法,n代表A、B两个矩阵是n阶方阵
Matrix temp;//临时矩阵,存放A*B结果
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
temp.s[i][j]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
temp.s[i][j]=(temp.s[i][j]+A.s[i][k]*B.s[k][j])%mod;
return temp;
}
Matrix quickPower(Matrix a,LL b,LL n){//矩阵快速幂,求矩阵n阶矩阵的b次幂
Matrix ans=e;
while(b){
if(b&1)
ans=mul(ans,a,n);//ans=e*a
a=mul(a,a,n);//a=a*a
b>>=1;
}
return ans;
}
int main(){
init();
LL a,b,n;
while(scanf("%lld%lld%lld%lld",&a,&b,&n,&mod)!=EOF){
if(n<=1)
printf("%lld\n",((2*a)%mod+mod)%mod);
else{
x.s[1][1]=2*a;x.s[1][2]=-(a*a-b);
x.s[2][1]=1;x.s[2][2]=0;
Matrix res=quickPower(x,n-1,2);
printf("%lld\n",((res.s[1][1]*2*a+res.s[1][2]*2)%mod+mod)%mod);
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)