HDU4565 So Easy! 矩阵高速幂外加数学
easy 个屁啊,一点都不easy,题目就是要求公式的值,但是要求公式在最后的取模前的值向上取整。再取模,无脑的先试了高速幂 double fmod来做,结果发现是有问题的。这题要做肯定得凑整数,凑整 题目给 a+√b 那么加上a-√b就能够了。但是这样加上后面怎么处理还得减去。想了半年也想不出来。
原来用了负数的共轭思想。还有就是题目给的b的范围 是 ((a-1)*(a-1),a*a)。所以 a-√b的值的 不管多少次方 的值都是小于1的,所以对于原式子 改装成
((a + √b) ^n+ (a - √b)^n)%MOD,这样由于(a + √b) ^n的值在取模前要向上取整么,所以加上了 (a - √b)^n 就是 答案了,特别变态。还得看b的范围来行事
最后在用 (a + √b) ^n+ (a - √b)^n 乘以 (a + √b)+ (a - √b)就能推出 (a + √b) ^(n+1) + (a - √b) ^(n+1) = 2 * a *((a + √b) ^n + (a - √b) ^n) - (a*a-b)*((a + √b) ^(n-1) + (a - √b) ^(n-1))
这样字的话 就有递推式可言了,就能构造矩阵来做了。最后还漏了负数的情况 ,搞的我敲了好几个小时。数学弱爆了。
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set> #define ll long long #define eps 1e-8 #define inf 0xfffffff const ll INF = 1ll<<61; using namespace std; //vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p; typedef struct Node {
int m[2][2];
}Matrix; Matrix per; int MOD; void init() {
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
per.m[i][j] = (i == j); } Matrix multi(Matrix a,Matrix b) {
Matrix c;
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
c.m[i][j] = 0;
for(int k=0;k<2;k++)
c.m[i][j] += a.m[i][k] * b.m[k][j];
c.m[i][j] %= MOD;
/*if(c.m[i][j] < 0) c.m[i][j] += MOD;*/
}
}
return c;
} Matrix quick(Matrix p,int k) {
Matrix ans = per;
while(k) {
if(k&1) {
ans = multi(ans,p);
k--;
}
else {
k >>= 1;
p = multi(p,p);
}
}
return ans;
} int main() {
int a,b,n;
init();
while(scanf("%d %d %d %d",&a,&b,&n,&MOD) == 4) {
Matrix ans;
memset(ans.m,0,sizeof(ans.m));
ans.m[1][0] = 2;
ans.m[0][0] = 2 * a;
if(n == 1) {
printf("%d\n",ans.m[0][0]%MOD);
continue;
}
Matrix tmp;
memset(tmp.m,0,sizeof(tmp.m));
tmp.m[0][0] = 2 * a%MOD;
tmp.m[0][1] = (-(a * a%MOD - b) + MOD)%MOD;//靠这里有负数要注意
tmp.m[1][0] = 1;
tmp.m[1][1] = 0;
/*Matrix bb = multi(tmp.tmp)*/
tmp = quick(tmp,n);
ans = multi(tmp,ans);
printf("%d\n",ans.m[1][0]);
}
return 0;
}
HDU4565 So Easy! 矩阵高速幂外加数学的更多相关文章
- hdu4565 So Easy! 矩阵快速幂
A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...
- HDU 2256 Problem of Precision(矩阵高速幂)
题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...
- HDOJ How many ways?? 2157【矩阵高速幂】
How many ways? ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- HDU3117-Fibonacci Numbers(矩阵高速幂+log)
题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解. 已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5 ...
- ZOJ 3690 & HDU 3658 (矩阵高速幂+公式递推)
ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
- HDU2842-Chinese Rings(递推+矩阵高速幂)
pid=2842">题目链接 题意:求出最少步骤解出九连环. 取出第k个的条件是,k-2个已被取出,k-1个仍在支架上. 思路:想必九连环都玩过吧,事实上最少步骤就是从最后一个环開始. ...
随机推荐
- JSON 字符串转换为 JavaScript 对象
将数据组合成json格式的字符串var text = '{ "sites" : [' + '{ "name":"Runoob" , &quo ...
- /etc/rsyncd.conf
[root@backup ~]# cat /etc/rsyncd.conf #Rsync server#created by oldboy ##rsyncd.conf start##uid = rsy ...
- pandas 1 基本介绍
import numpy as np import pandas as pd pd.Series() 构造数据 s = pd.Series([1, 3, 5, np.nan, 44, 1]) prin ...
- react添加右键点击事件
1.在HTML里面支持contextmenu事件(右键事件).所以需要在组建加载完时添加此事件,销毁组建时移除此事件. 2. 需要增加一个state,名称为visible,用来控制菜单是否显示.在_h ...
- C语言回调
来源:https://www.cnblogs.com/jiangzhaowei/p/9129105.html 1. 什么是回调函数? 回调函数,光听名字就比普通函数要高大上一些,那到底什么是回调函数呢 ...
- ASP.NET-webconfig中注意事项
正确的写法是这样的 正常解析含有html的代码需要@Html.Raw(ViewBag.ss); 直接使用@ViewBag.ss来显示只能显示源代码,像这样 来自为知笔记(Wiz)
- swift入门-实现简单的登录界面
// // AppDelegate.swift // UIWindow import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- Oracle单实例情况下的library cache pin的问题模拟与问题分析
Oracle单实例情况下的library cache pin的问题模拟与问题分析 參考自: WAITEVENT: "library cache pin" Reference Not ...
- [学习笔记]BS架构与CS架构
整理自:http://www.iteye.com/problems/102411 前两天面试的时候被问到这个问题,没有回答上来因此在这里学习整理一下. B/S架构 B/S架构的全称为Browser/S ...
- jsp输出金字塔
<% String str = ""; for(int i = 1; i <= 5; i++){ for(int j = 1; j <= 5-i; j++){ s ...