題目連接:http://codeforces.com/problemset/problem/1176/Ahtml
題意:ios
給定一個數字 \(n\) 和三種操做c++
求問 \(n\) 最少執行多少次能變成 \(1\) ,若是不行則輸出 \(-1\)ide
//搜索 // Author : RioTian // Time : 20/10/15 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll _, n, cnt; void dfs(ll x, ll step) { if (x == 1) { cnt = min(step, cnt); return; } if (x % 2 == 0) dfs(x / 2, step + 1); else if (x % 3 == 0) dfs(x * 2 / 3, step + 1); else if (x % 5 == 0) dfs(x * 4 / 5, step + 1); else cnt = -1; } int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> _; while (_--) { cin >> n, cnt = 9999999; if (n == 1) cnt = 0; else dfs(n, 0); cout << cnt << endl; } }
//math #include <bits/stdc++.h> using namespace std; int64_t n, i, q; int main() { for (cin >> q; q--;) { cin >> n; for (i = 0; n > 1 && i < 200; i++) n % 2 == 0 ? n /= 2 : n % 3 == 0 ? n = 2 * n / 3 : n % 5 == 0 ? n = 4 * n / 5 : 0; cout << (n > 1 ? -1 : i) << endl; } }
題目連接:http://codeforces.com/problemset/problem/1176/Bspa
題意:給定序列,任意倆個元素能夠相加成一個元素,求序列元素能被3整除的最大數量。code
思路: 對於全部元素進行 模3 的預處理,而後 貪心 餘數1 和 餘數2 的配對,剩下的 3個 一組配對。htm
AC代碼:blog
// Author : RioTian // Time : 20/10/15 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { // freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; scanf("%d", &T); while (T--) { int n, t, a[3] = {0}; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &t); a[t % 3]++; } printf("%d\n", a[0] + min(a[1], a[2]) + (max(a[1], a[2]) - min(a[1], a[2])) / 3); } }
題目連接:https://codeforces.com/problemset/problem/1176/Cci
題意:保證這個順序 4,8,15,16,23,42 ,刪除其餘多餘的數據,計算要刪除多少個數字,才能保證剩下的數字能組成n套這樣的(n≥1);get
題解:遍歷序列,若是這個數爲n,且這個數前面的數字爲1,則b[n++],b[前面那個數字]–。
例子:
12
4 8 4 15 16 8 23 15 16 42 23 42
遍歷這個序列:
第一個數字是4,則b[4++];
第二個數字是8,則b[8++],b[4–];由於順序是規定的,因此有8,必定有4。
第三個數字是4,則b[4++];
第四個數字是15,則b[15++],b[8–];
第五個數字是16,則b[16++],b[15–];若是有16必定有15,因此能夠用16代替前面的全部數字。
以此類推。(建議在紙上畫一下就能夠理解了。)
// Author : RioTian // Time : 20/10/15 #include <bits/stdc++.h> #define ms(a, b) memset(a, b, sizeof a) using namespace std; typedef long long ll; const int N = 5e5 + 100; int n, m, a[N], ans[] = {4, 8, 15, 16, 23, 42}, vis[10]; map<int, int> mp; void init() { mp[4] = 1; mp[8] = 2; mp[15] = 3; mp[16] = 4; mp[23] = 5; mp[42] = 6; ms(vis, 0); } int main() { //freopen("in.txt", "r", stdin); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); while (cin >> n) { init(); for (int i = 1; i <= n; i++) { cin >> m; if (mp[m] == 1) vis[1]++; else if (vis[mp[m] - 1] > 0) { vis[mp[m] - 1]--; vis[mp[m]]++; } } if (n < 6) cout << n << endl; else cout << n - vis[6] * 6 << endl; } return 0; }
https://codeforces.com/problemset/problem/1176/D
題意:給出b[n],按照必定的規則變成a[n];
題解:
原理是:倒推,本來題意是,由a[n]變成b[n];
遍歷:
若是a[n]是質數,那麼在b[n]中,保留a[n]而且加上質數表中的第a[n]個質數;
若是a[n]是合數,那麼在b[n]中,保留a[n]而且加上b[n]的最大除數。
#include <bits/stdc++.h> using namespace std; #define N 3000000 int n, i, j, k, x, p[N], q[N], c[N], b[N]; int main() { for (i = 2; i < N; i++) if (!p[i]) { q[++k] = i; for (j = i + i; j < N; j += i) p[j] = 1; } for (cin >> n, i = 0; i < n + n;) cin >> b[i], c[b[i++]]++; sort(b, b + n + n); for (i = n + n - 1; i + 1; i--) { x = b[i]; if (c[x] <= 0 || !p[x]) continue; for (j = 2; x % j; j++) ; cout << x << " ", c[x]--, c[x / j]--; } for (i = 0; i < n + n; i++) if (x = b[i], c[x] > 0) cout << x << " ", c[x]--, c[q[x]]--; return 0; }
https://codeforc.es/contest/1176/problem/E
久了不寫bfs了。一開始用dfs寫,的確用dfs是頗有問題的,一些奇怪的狀況就會致使多染一些色。
注意無向圖的邊要開雙倍。
//DFS 修改之後AC的代碼 #include<bits/stdc++.h> const int maxn = 2e5+10; using namespace std; vector<int> V[maxn],ans[2]; int n,m; int T; int vis[maxn]; void dfs(int u,int ind) { vis[u]=1; ans[ind].push_back(u); for(auto nx:V[u]) { if(vis[nx]) continue; dfs(nx,ind^1); } return; } int main() { scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;++i) {V[i].clear();vis[i]=0;} ans[0].clear();ans[1].clear(); for(int i=1;i<=m;++i) { int a,b; scanf("%d%d",&a,&b); V[a].push_back(b); V[b].push_back(a); } dfs(1,0); if(ans[0].size()>ans[1].size()) { swap(ans[0],ans[1]); } printf("%lu\n",ans[0].size()); for(auto x:ans[0]) printf("%d ",x); puts(""); } return 0; }