[洛谷11月月賽]
阿新 • • 發佈:2018-11-04
比賽連結
T1
思路
按照斐波那契的式子到著往前推就行,f[i]=f[i+2] - f[i+1],當找到某個值使得f[i] = 0,f[i+1] = 1的時候就停止。
程式碼
//https://www.luogu.org/problemnew/show/P4994 #include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; ll read() { ll x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int main() { int ans = 0; int m = read(),now = 1, lst = 0; while(now != 0 || lst != 1) { ans++; int k = now; now = (lst - now + m) % m; lst = k; } cout<<ans + 1; return 0; }
T2
思路
將序列排序,然後每次從一段跳另一端。
程式碼
#include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N = 310; ll read() { ll x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int a[N]; int main() { int n = read(); for(int i = 1;i <= n;++i) a[i] = read(); int l = 0,r = n; sort(a,a+n+1); int bz = 0; ll ans = 0; while(l < r) { ans += (a[r] - a[l]) * (a[r] - a[l]); if(bz & 1) r--; else l++; bz ^= 1; } cout<<ans; return 0; }
T3
思路
沒看懂題意,所以去瞄(chao)了一眼(fa)題解。
大概思路就是,計算出從全0到這個狀態有多少種情況,再計算出從這個狀態到全1有多少種情況,將他們與這個狀態的歉意值乘起來,就是這個狀態整個的貢獻。
因為一個狀態1的位置是無所謂的,所以只看1的個數即可。用f[i]表示從0到i個1的時候的情況數量。列舉j表示最後一次放了多少個1,\(f[i] = \sum\limits_{j=1}^i{f[i-1]*C(_i^j)}\)
程式碼
#include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N = 30,mod = 998244353; ll read() { ll x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } ll f[N]; ll C[N][N]; int n, m; void pre() { C[0][0] = 1; for(int i = 1;i <= n; ++i) { C[i][0] = 1; for(int j = 1;j <= i;++j) { C[i][j] = C[i-1][j-1] + C[i-1][j]; C[i][j] >= mod ? C[i][j] -= mod : 0; } } f[0] = 1; for(int i = 1;i <= n;++i) { for(int j = 1;j <= i;++j) { f[i] += f[i-j] * C[i][j] % mod; f[i] >= mod ? f[i] -= mod : 0; } } } char s[N]; ll ans; int main() { n = read(),m = read(); pre(); while(m--) { scanf("%s",s + 1); int w = read(); int tot = 0; for(int i = 1;i <= n;++i) { if(s[i] == '1') tot++; } ans += f[tot] * f[n-tot] % mod * w % mod; ans >= mod ? ans-= mod : 0; } cout<<ans; return 0; }