#include<bits/stdc++.h> #define ll long long #define INF 1e9 usingnamespace std; unsigned ll n; vector <int> ans; voidbin(vector <int>& A, unsigned ll n){ do { A.push_back(n & 1); n >>= 1; } while (n); } intmain(){ ios :: sync_with_stdio(0); cin >> n; bin(ans, n); for (int i = 0; i < ans.size(); i++) cout << ans[i]; return0; }
快速幂
1 2 3 4 5 6 7 8 9 10 11 12
#include<bits/stdc++.h> #define ll long long #define INF 1e9 usingnamespace std; int n, m, p; intqpow(ll n, ll m, int p){ return !m ? 1 : ((m & 1 ? n : 1) * (m = qpow(n, m >> 1, p)) % p * m % p); } intmain(){ ios :: sync_with_stdio(0); cin >> n >> m >> p; cout << qpow(n, m, p); return0; }
#include<bits/stdc++.h> #define ll long long #define INF 1e9 usingnamespace std; int n, ans = -INF; int a[5005], dp[5005]; intmain(){ ios :: sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j <= i; j++) { if (a[j] < a[i]) dp[i] = max(dp[i], dp[j] + 1); } ans = max(ans, dp[i]); } cout << ans; return0; }
最长上升子序列 / LIS (二分优化)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<bits/stdc++.h> #define ll long long #define INF 1e9 usingnamespace std; int n, ans; int a[100005], g[100005]; intmain(){ ios :: sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) g[i] = INF; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { g[lower_bound(g + 1, g + ans + 1, a[i]) - g] = a[i]; ans = max(ans, (int)(lower_bound(g + 1, g + ans + 1, a[i]) - g)); } cout << ans; return0; }