码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ4002 [JLOI2015]有意义的字符串

时间:2015-04-22 22:05:23      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

据说这两场加起来只要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$,否则不用减

 

技术分享
 1 /**************************************************************
 2     Problem: 4002
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:48 ms
 7     Memory:1272 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11 #include <iostream>
12 #include <cstring>
13 #include <algorithm>
14  
15 using namespace std;
16 typedef unsigned long long ll;
17 const ll mod = 7528443412579576937ull;
18  
19 template <class T> T sqr(T x) {
20     return x * x;
21 }
22  
23 template <class T> T mul(T x, T y) {
24     static T res;
25     if (x < y) swap(x, y);
26     res = 0;
27     while (y) {
28         if (y & 1) res = (res + x) % mod;
29         x = (x << 1) % mod, y >>= 1;
30     }
31     return res;
32 }
33  
34 ll b, d, n;
35  
36 struct mat {
37     ll x[3][3];
38      
39     inline void clear() {
40         memset(x, 0, sizeof(x));
41     }
42     inline void one() {
43         memset(x, 0, sizeof(x));
44         x[1][1] = x[2][2] = 1;
45     }
46     inline void pre() {
47         this -> clear();
48         x[1][1] = 0, x[1][2] = (d - sqr(b)) >> 2, x[2][1] = 1, x[2][2] = b;
49     }
50      
51     inline ll* operator [] (int i) {
52         return x[i];
53     }
54      
55     inline mat operator * (const mat &p) const {
56         static mat res;
57         static int i, j, k;
58         for (res.clear(), i = 1; i <= 2; ++i)
59             for (j = 1; j <= 2; ++j)
60                 for (k = 1; k <= 2; ++k)
61                     res[i][j] = (res[i][j] + mul(x[i][k], p.x[k][j])) % mod;
62         return res;
63     }
64     inline mat& operator *= (const mat &p) {
65         return *this = *this * p;
66     }
67 } a;
68  
69 inline mat pow(const mat &p, ll y) {
70     static mat x, res;
71     res.one(), x = p;
72     while (y) {
73         if (y & 1) res *= x;
74         x *= x, y >>= 1;
75     }
76     return res;
77 }
78  
79 int main() {
80     cin >> b >> d >> n;
81     a.pre();
82     a = pow(a, n);
83     cout << (((a[1][1] << 1) % mod + mul(b, a[2][1]) - (d != sqr(b) && !(n & 1))) % mod + mod) % mod << endl;
84     return 0;
85 }
View Code

 

BZOJ4002 [JLOI2015]有意义的字符串

标签:

原文地址:http://www.cnblogs.com/rausen/p/4448713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!