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++)//主对角线为1e.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*aa=mul(a,a,n);//a=a*ab>>=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;
}
So Easy!(HDU - 4565)相关推荐
- 有源汇有上下界最大流/最小流 配题(HDU 3157)
因为是有源汇所以设源点为 s,汇点为 t. 有源汇有上下界最大流: 连接一条 t 指向 s 的边,容量为 INF. 通过上述步骤,现在图变成了无源汇网络. 引入超级源点 S,超级汇点 T. 连接一条 ...
- 最大表示法--环形字符串最大字典序(HDU 5442)
http://acm.hdu.edu.cn/showproblem.php?pid=5442 问题概述:n个字符围成一个环,请从这个环中找出字典序最大的长度为n的字符串,输出它的起始点和方向(0顺1 ...
- S-Nim (HDU 1536)组合博弈SG多组游戏
S-Nim 题目链接 Problem Description Arthur and his sister Caroll have been playing a game called Nim for ...
- HDU2019多校第二场 1009(HDU 6599) I Love Palindrome String(回文树(自动机)+manacher)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 解题思路: 回文自动机求每个本质不同的子串出现的次数,同时记录每个节点i代表的回文串第一次出现的 ...
- BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...
- LeetCode算法题-Goat Latin Easy(Java实现)
这是悦乐书的第322次更新,第344篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第192题(顺位题号是824).给出句子S,由空格分隔的单词组成.每个单词仅由小写和大写 ...
- (HDU - 1847)Good Luck in CET-4 Everybody!(博弈)
题目链接:Good Luck in CET-4 Everybody! - HDU 1847 - Virtual Judge (ppsucxtt.cn) 题目是中文的,我在这就不翻译题意了. 先说一种打 ...
- 美素数(HDU 4548)(打表,简化时间复杂度)
相信大家都喜欢美的东西,让我们一起来看看美素数吧. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数",如29,本身是素数,而且2+9 = 11 ...
- 单词数(HDU 2072)
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...
最新文章
- Node webkit启动最大化窗口
- Samba服务搭建及详解
- 空车上路,Waymo拿下加州首个“真”无人驾驶许可证
- vendor自动恢复_解决 vendor 中存在大小写变更问题
- 高精度算法(加减乘除取模(均可以处理负数))
- 【Post工具】PostMan 他媳妇 PostWoman
- 小时级的进度监控工具
- 25 PP模块-创建工厂日历报错-请输入年度xxxx和xxxx之间的有效区域
- mongodb查询文件服务器的数据,服务器端知识库mongodb基础篇
- 利用Jmail发送带附件的邮件时乱码的解决方案
- JDK = JRE + 编译器 + api + tools
- C语言:基于Easyx库实现连连看小游戏
- 关于在Ubuntu中更新pip时遇到的问题及解决方法
- (转)yolov3运行及保存检测视频(包括摄像头)
- 开始学习机器学习之前你必须要了解的知识有哪些?机器学习系列入门篇
- ros平台下python脚本控制机械臂运动
- R语言七天入门教程二:认识变量与运算符
- 记一次jenkins 构建go项目经历
- 大数据开发学习:进行大数据开发课程有哪些
- 车载以太网技术(一)