BZOJ4002 [JLOI2015]有意义的字符串
据说这两场加起来只要170= =而这是最简单的题目了QAQ
看到$(\frac {b + \sqrt {d} } {2} )^n$,第一反应是共轭根式$(\frac {b - \sqrt {d} } {2} )^n$
首先有$(\frac {b + \sqrt {d} } {2} )^n + (\frac {b - \sqrt {d} } {2} )^n$为整数
由高中课本知识可知,上式其实是一个三项递推数列的通项公式,而数列的递推式非常简单
$$f[x] = b * f[x - 1] - \frac{b^2 - d} {4} * f[x - 2]$$
其中$f[0] = 2, f[1] = b$,这样子直接矩阵乘法就好啦~
再看$A = (\frac {b - \sqrt {d} } {2} )^n \in [-1, 1]$
然后。。。然后发现bzoj上题面错了QAQ,实际上是"对于20%的数据$b=1,d=5$"
故$A > 0$当且仅当$d \not= b^2$且$n$为偶数,此时要将$f[n]$减掉$1$,否则不用减
/**************************************************************
Problem: 4002
User: rausen
Language: C++
Result: Accepted
Time:48 ms
Memory:1272 kb
****************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std;
typedef unsigned long long ll;
const ll mod = 7528443412579576937ull; template <class T> T sqr(T x) {
return x * x;
} template <class T> T mul(T x, T y) {
static T res;
if (x < y) swap(x, y);
res = ;
while (y) {
if (y & ) res = (res + x) % mod;
x = (x << ) % mod, y >>= ;
}
return res;
} ll b, d, n; struct mat {
ll x[][]; inline void clear() {
memset(x, , sizeof(x));
}
inline void one() {
memset(x, , sizeof(x));
x[][] = x[][] = ;
}
inline void pre() {
this -> clear();
x[][] = , x[][] = (d - sqr(b)) >> , x[][] = , x[][] = b;
} inline ll* operator [] (int i) {
return x[i];
} inline mat operator * (const mat &p) const {
static mat res;
static int i, j, k;
for (res.clear(), i = ; i <= ; ++i)
for (j = ; j <= ; ++j)
for (k = ; k <= ; ++k)
res[i][j] = (res[i][j] + mul(x[i][k], p.x[k][j])) % mod;
return res;
}
inline mat& operator *= (const mat &p) {
return *this = *this * p;
}
} a; inline mat pow(const mat &p, ll y) {
static mat x, res;
res.one(), x = p;
while (y) {
if (y & ) res *= x;
x *= x, y >>= ;
}
return res;
} int main() {
cin >> b >> d >> n;
a.pre();
a = pow(a, n);
cout << (((a[][] << ) % mod + mul(b, a[][]) - (d != sqr(b) && !(n & ))) % mod + mod) % mod << endl;
return ;
}
BZOJ4002 [JLOI2015]有意义的字符串的更多相关文章
- BZOJ4002 [JLOI2015]有意义的字符串 【数学 + 矩乘】
题目链接 BZOJ4002 题解 容易想到\(\frac{b + \sqrt{d}}{2}\)是二次函数\(x^2 - bx + \frac{b^2 - d}{4} = 0\)的其中一根 那么就有 \ ...
- [BZOJ4002][JLOI2015]有意义的字符串-[快速乘法+矩阵乘法]
Description 传送门 Solution 由于这里带了小数,直接计算显然会爆掉,我们要想办法去掉小数. 而由于原题给了暗示:b2<=d<=(b+1)2,我们猜测可以利用$(\fra ...
- bzoj4002 [JLOI2015]有意义的字符串 快速幂
Description B 君有两个好朋友,他们叫宁宁和冉冉. 有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求((b+sqrt(D)/2)^N的整数部分,请输出结果 Mod 752844341 ...
- bzoj4002 [JLOI2015]有意义的字符串 特征根+矩阵快速幂
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4002 题解 神仙题. 根据下面的一个提示: \[ b^2 \leq d \leq (b+1)^ ...
- 【BZOJ4002】[JLOI2015]有意义的字符串(数论,矩阵快速幂)
[BZOJ4002][JLOI2015]有意义的字符串(数论,矩阵快速幂) 题面 BZOJ 洛谷 题解 发现我这种题总是做不动... 令\(A=\frac{b+\sqrt d}{2},B=\frac{ ...
- 【BZOJ4002】[JLOI2015]有意义的字符串 数学
[BZOJ4002][JLOI2015]有意义的字符串 Description B 君有两个好朋友,他们叫宁宁和冉冉.有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求 Input 一行三个整数 ...
- BZOJ_4002_[JLOI2015]有意义的字符串_矩阵乘法
BZOJ_4002_[JLOI2015]有意义的字符串_矩阵乘法 Description B 君有两个好朋友,他们叫宁宁和冉冉.有一天,冉冉遇到了一个有趣的题目:输入 b;d;n,求 Input 一行 ...
- [JLOI2015]有意义的字符串
4002: [JLOI2015]有意义的字符串 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1000 Solved: 436[Submit][St ...
- 【BZOJ4002】[JLOI2015]有意义的字符串 - 矩阵乘法
题意: 给出b,d,n,求$\lfloor(\frac{b+\sqrt{d}}{2})^n\rfloor \mod 999999999999999989$(原题是7528443412579576937 ...
随机推荐
- 发布《Linux工具快速教程》
发布<Linux工具快速教程> 阶段性的完成了这本书开源书籍,发布出来给有需要的朋友,同时也欢迎更多的朋友加入进来,完善这本书: 本书Github地址:https://github.com ...
- sqlserver前面加N解释
From : http://lzde360.blog.163.com/blog/static/6780720820111026112033917/ 加上 N 代表存入数据库时以 Unicode 格式存 ...
- chubu
python解释型语言,不需要编译成机器认可的二进制码,而是直接从源代码运行程序. python是基于c语言开发的. python很容易嵌入到其他语言. 中文注释,必须在前边加上注释说明 : #_*_ ...
- iOS - OC 异常处理
1.@try 语句 @try { // Code that can potentially throw an exception 可能会抛出异常的代码块 statement . . . } @catc ...
- spring事务知识
事务的传播行为? 在Spring 的事务中, _可以通过 propagation 来定义事务的传播行为_: PROPAGATION_required:如果当前没有事务,就新建一个事务,如果已经存在一个 ...
- mac下pmset的使用方法
在终端输入:pmset -g custom 这个命令可以显示系统所有的电源设置 lidwake: 当屏幕掀开的时候唤醒Mac,1是开启 0是关闭autopoweroff: 如果Mac处于睡眠状 ...
- linux中脚本的一些小知识的积累
对于变量的问题: 对变量赋值,a="hello world",现在打印变量a的内容:echo $a. 对于${}的使用:如$aall,我们想要$a,这是,就可以${a}all了. ...
- mustache.js渲染带事件的模板
http://zccst.iteye.com/blog/2183111 最近遇到这样一个问题,预览内容可点击,问题是通过$.Mustache.render("templateId" ...
- [转]C++中引用(&)的用法和应用实例
from: here 对于习惯使用C进行开发的朋友们,在看到c++中出现的&符号,可能会犯迷糊,因为在C语言中这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符 ...
- 转:随机函数 C++中rand()函数的用法
转自:http://blog.163.com/wujiaxing009@126/blog/static/719883992011113011359154/ 一.C++中不能使用random()函数 ...