标签:wing nta title iostream repeat-y rri line iss add
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 576 Accepted Submission(s): 390
pell方程的的入门题
求第k大
先求特解 然后根据
用矩阵快速幂就好了
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define rb(a) scanf("%lf", &a) #define rf(a) scanf("%f", &a) #define pd(a) printf("%d\n", a) #define plld(a) printf("%lld\n", a) #define pc(a) printf("%c\n", a) #define ps(a) printf("%s\n", a) #define MOD 8191 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff; int tot = 1; struct Matrix { LL v[110][110]; Matrix() { memset(v, 0, sizeof(v)); } Matrix operator *(const Matrix B) { int i, j, k; Matrix C; for(i = 0; i <= tot; i ++) for(j = 0; j <= tot; j ++) for(k = 0; k <= tot; k ++) { C.v[i][j] = (C.v[i][j] + v[i][k] * B.v[k][j]) % MOD; } return C; } }; Matrix mtPow(Matrix A, int k) { int i; Matrix B; for(i = 0; i <= tot; i ++) { B.v[i][i] = 1; } while(k) { if(k & 1) B = B * A; A = A * A; k >>= 1; } return B; } int main() { LL n, k; while(cin >> n >> k) { LL t = sqrt(n); if(t * t == n) { cout << "No answers can meet such conditions" << endl; continue; } LL x, y = 1; while(1) { x = sqrt(n * y * y + 1); if(x * x == n * y * y + 1) break; y++; } Matrix A; A.v[0][0] = A.v[1][1] = x % MOD; A.v[0][1] = (n * y) % MOD, A.v[1][0] = y % MOD; Matrix B = mtPow(A, k - 1); cout << ((B.v[0][0] * x) % MOD + (B.v[0][1] * y) % MOD) % MOD << endl; } return 0; }
No more tricks, Mr Nanguo HDU - 3292(pell + 矩阵快速幂)
标签:wing nta title iostream repeat-y rri line iss add
原文地址:https://www.cnblogs.com/WTSRUVF/p/10294315.html