題解:
這題注意看a[i]的數值範圍,處理一下前綴和後綴中的最小值便可,不在這個區間中的最小值即min(前綴中的最小值, 後綴中的最小值)ios
AC代碼:c++
#include<iostream> #include<stdio.h> #include<algorithm> #include<queue> #include<map> using namespace std; const int N = 1e5 + 15; int a[N]; int L[N], R[N]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; L[0] = R[n + 1] = n; for (int i = 1; i <= n; i++) L[i] = min(L[i - 1], a[i]); for (int i = n; i >= 0; i--) R[i] = min(R[i + 1], a[i]); while (q--) { int l, r; cin >> l >> r; cout << min(L[l - 1], R[r + 1]) << endl; } return 0; }
題解:
碰見這種題最主要的就是化簡式子,列舉前幾項,找規律化簡
n = 1:res = 1 * 1 * 1
n = 2:res = (1 * 1 * 1) * (2 * 1 * 1 + 2 * 2 * 1 + 2 * 2 * 2)
n = 3:res = (1 * 1 * 1) * (2 * 1 * 1 + 2 * 2 * 1 + 2 * 2 * 2) * (3 * 1 * 1 + 3 * 2 * 1 + 3 * 2 * 2 + 3 * 3 * 1 + 3 * 3 * 2 + 3 * 3 * 3)
······
嘗試化簡:(對n = 3時)
一、對每一項化簡,提出一個i的(1 * 2 * 3) * (1 * 1 + 2 * (1 + 2))* (1 * 1 + 2 * (1 + 2)+ 3 * (1 + 2 + 3))
二、獲得:res = n! * (1 * sum[1]) * (1 * sum[1] + 2 * sum[2]) * (1 * sum[1] + 2 * sum[2] + 3 * sum[3])
由於第一項是n的階乘,因此當n >= 199999時,取模以後res = 0;
複雜度 O(n)web
AC代碼:svg
#include<iostream> #include<stdio.h> #include<algorithm> #include<queue> #include<map> #include<string> using namespace std; #define ll long long const int mod = 199999; ll nsum[mod + 15]; void init() { ll mulsum = 0, sum = 0; nsum[0] = 1; for (int i = 1; i <= mod + 1; i++) { sum = (sum + i) % mod; mulsum = (mulsum + sum * i) % mod; nsum[i] = i * ((nsum[i - 1] * (mulsum % mod)) % mod) % mod; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); init(); int t; cin >> t; while (t--) { string n; cin >> n; int res = 0; int flag = 0; for (int i = 0; i < n.length(); i++) { res = res * 10 + (n[i] - '0'); if (res >= mod) { flag = 1; break; } } if (flag) cout << 0 << endl; else { cout << nsum[res] % mod << endl; } } return 0; }