CSUOJ 1895 Apache is late again
Description
Apache is a student of CSU. There is a math class every Sunday morning, but he is a very hard man who learns late every night. Unfortunate, he was late for maths on Monday. Last week the math teacher gave a question to let him answer as a punishment, but he was easily resolved. So the math teacher prepared a problem for him to solve. Although Apache is very smart, but also was stumped. So he wants to ask you to solve the problem. Questions are as follows:You can find a m made (1 + sqrt (2)) ^ n can be decomposed into sqrt (m) + sqrt (m-1), if you can output m% 100,000,007 otherwise output No.
Input
There are multiply cases.Each case is a line of n. (|n| <= 10 ^ 18)
Output
Line, if there is no such m output No, otherwise output m% 100,000,007.
Sample Input
2
Sample Output
9
Hint
[an an-1]T =[ (2 1) (1 0)]*[an-2 an-1]T=[(2 1) (1 0)]^n-2 *[a2 a1]
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int MOD = 100000007; typedef long long ll; struct matrix{ ll v[2][2]; matrix() { memset(v, 0, sizeof(v)); } matrix operator*(const matrix &m) { matrix c; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { c.v[i][j] += (v[i][k] * m.v[k][j]) % MOD; } } } return c; } }; matrix E, M, ans; void init() { for (int i = 0; i < 2; i++) E.v[i][i] = 1; M.v[0][0] = 2; M.v[0][1] = 1; M.v[1][0] = 1; M.v[1][1] = 0; } matrix quick_pow(matrix x, ll y) { matrix tmp = E; while (y) { if (y & 1) { tmp =tmp* x; y--; } y >>= 1; x = x*x; } return tmp; } int main() { ll n; init(); while (~scanf("%lld", &n)) { if (n < 0) printf("No\n"); else if (n == 0) printf("1\n"); else if (n == 1) printf("2\n"); else if (n == 2) printf("9\n"); else { ans = quick_pow(M, n - 2); ll a = (ans.v[0][0] * 3 + ans.v[0][1]) % MOD; if (n & 1) printf("%lld\n", ((a*a)%MOD + 1) % MOD); else printf("%lld\n", (a*a)%MOD); } } return 0; } /********************************************************************** Problem: 1895 User: leo6033 Language: C++ Result: AC Time:8 ms Memory:2024 kb **********************************************************************/