5 条题解
-
0
来个矩阵快速幂(虽然通常是需要取模的时候再用😋o(logn))
#include <bits/stdc++.h> using namespace std; #define ll long long typedef vector<vector<ll>> mat; mat operator*(const mat &a, const mat &b) { mat c(2, vector<ll>(2)); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } mat operator^(mat a, ll b) { mat res = {{1, 0}, {0, 1}}; while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } void solve() { ll n; cin >> n; if (n == 1) { cout << 1 << '\n'; return; } mat trans = {{1, 1}, {1, 0}}; mat power = trans ^ (n - 2); mat init = {{1, 0}, {1, 0}}; mat res = power * init; cout << res[0][0] << '\n'; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
-
0
用递归做是最简单的
#include <bits/stdc++.h> using namespace std; int dg(int n) { if(n==1||n==2) { return 1;//如果是1/2,直接return1 } else { return dg(n-1)+dg(n-2);//否则继续递归 } } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int n,m; cin>>n; for(int i=1;i<=n;i++) { cin>>m; cout<<dg(m)<<'\n'; } return 0; }
- 1
信息
- ID
- 123
- 时间
- 3000ms
- 内存
- 128MiB
- 难度
- 4
- 标签
- (无)
- 递交数
- 1017
- 已通过
- 443
- 上传者