hnu13150(hdu 4565)SO EASY!(矩阵快速幂)

2024-06-15 19:38

本文主要是介绍hnu13150(hdu 4565)SO EASY!(矩阵快速幂),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

So Easy!
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 18, Accepted users: 8
Problem 13150 : No special judgement
Problem description

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! 


Input

There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.
The input will finish with the end of file.


Output

For each the case, output an integer Sn.


Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
Sample Output
4
14
4
Problem Source
HNU Contest 

原来博士出的SO EASY就是这个题。。。之前听过了但是没做过

之前做过一个类似的  所以一看到就知道是用矩阵快速幂做

但是这个多了一个取上界 第一直觉就是按之前的想法求值之后加一

照这个想法写了之后 跑了样例  一样。。。就交了  发现WA了

就意识到应该是那个上界的处理上出了问题


因为  ceil((a+sqrt(b))^n)=(a+sqrt(b))^n+(a-sqrt(b)^n)

令 a+sqrt(b)=x  a-sqrt(b)=y

(a+sqrt(b))^n+(a-sqrt(b)^n)=x^n+y^n=(x+y)(x^(n-1)+y^(n-1))-x*y(x^(n-2)+y^(n-2))

设 g(n)=x^n+y^n   x+y=2*a=p x*y=a*a-b=q

|g(n) g(n-1)|=|g(n-1) g(n-2)|* |p 1|

      |-q 0|

然后就可以由g(1) g(0) 2阶矩阵的n-1次幂求值

g(1)=2*a=p  g(0)=2

注意数据范围 所有的都要是__int64

代码如下:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
//#define MOD 10009
#define MAXN 10010
#define INF 0x7fffffff
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)using namespace std;ll mod,c,d;struct matrix
{ll a[2][2];
};matrix ori,res;void init()
{res.a[0][1]=res.a[1][0]=0;res.a[0][0]=res.a[1][1]=1;
}matrix multiply(matrix x,matrix y)
{matrix temp;memset(temp.a,0,sizeof(temp.a));for(int i=0;i<2;i++)for(int j=0;j<2;j++)for(int k=0;k<2;k++){temp.a[i][j]+=x.a[i][k]*y.a[k][j];temp.a[i][j]%=mod;}return temp;
}void calc(ll n)
{while(n){if(n&1)   res=multiply(ori,res);n>>=1;ori=multiply(ori,ori);}
}int main()
{
//    fread;ll n;while(scanf("%I64d%I64d%I64d%I64d",&c,&d,&n,&mod)!=EOF){init();ll x=2*c;ll y=c*c-d;ori.a[0][0]=x;ori.a[1][1]=0;ori.a[0][1]=1;ori.a[1][0]=-y;
//        ori.a[0][0]=ori.a[1][1]=c;
//        ori.a[0][1]=d;
//        ori.a[1][0]=1;calc(n-1);ll ans;ans=((x*res.a[0][0]+2*res.a[1][0])%mod+mod)%mod;//        double ans=0.0;
//        ll an=res.a[0][0]*c%mod+res.a[0][1]%mod;
//        an%=mod;
//        ll bn=res.a[1][0]*c%mod+res.a[1][1]%mod;
//        bn%=mod;
//        ans+=(double)1.0*res.a[0][0]*c%mod;
//        ans+=(double)1.0*res.a[0][1]%mod;
//        ans+=(double)(res.a[1][0]*c*1.0+1.0*res.a[1][1])*sqrt((double)5)%mod;
//        ans=ceil((double)((double)an+(double)bn*1.0*sqrt((double)d)));
//        printf("%llf\n",ans);
//        ans=(double)an+(double)bn*1.0*sqrt((double)d);
//        ll pp=(int)ans;
//        pp++;
//        pp%=mod;printf("%I64d\n",ans);}return 0;
}




这篇关于hnu13150(hdu 4565)SO EASY!(矩阵快速幂)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064393

相关文章

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav

Python如何快速下载依赖

《Python如何快速下载依赖》本文介绍了四种在Python中快速下载依赖的方法,包括使用国内镜像源、开启pip并发下载功能、使用pipreqs批量下载项目依赖以及使用conda管理依赖,通过这些方法... 目录python快速下载依赖1. 使用国内镜像源临时使用镜像源永久配置镜像源2. 使用 pip 的并

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import