// 答案是 3181#include<iostream>usingnamespace std;int nums[15];boolcheck(int x){while(x){int now = x %10;x /=10;if(nums[now]<1)returnfalse;nums[now]--;}returntrue;}intmain(){for(int i =0; i <=9; i ++) nums[i]=2021;for(int i =1;; i ++){if(!check(i)){cout << i -1;return0;}}}
試題C :直線
需要注意當垂直于x軸使無法計算斜率
注意計算b時,式子中不要涉及到k,可以用整型,就不要用浮點數計算
// 答案是40257#include<iostream>#include<map>usingnamespace std;constint N =30*30;struct Point
{int x, y;}p[N];int cnt;map<pair<double,double>,int> ma;intmain(){for(int i =0; i <20; i ++)for(int j =0; j <21; j ++){p[cnt].x = i; p[cnt].y = j; cnt ++;}int res =20+21;for(int i =0; i < cnt; i ++)for(int j =0; j < cnt; j ++){int x1 = p[i].x, y1 = p[i].y, x2 = p[j].x, y2 = p[j].y;if(x1 == x2 || y1 == y2)continue;double k =1.0*(y2 - y1)/(x2 - x1);double m =1.0*(x2 * y1 - x1 * y2)/(x2 - x1);if(ma[{k, m}]==0){res ++;ma[{k, m}]=1;}}cout << res;}
// 答案是2430#include<iostream>#include<vector>usingnamespace std;typedeflonglong ll;const ll n =2021041820210418;intmain(){ll res =0;vector<ll> ve;for(ll i =1; i * i <= n; i ++){if(n % i ==0){ve.push_back(i);if(i * i != n) ve.push_back(n / i);}}for(auto i : ve)for(auto j : ve)for(auto k : ve)if(i * j * k == n)res ++;cout << res;}
試題E :路徑
// 答案是10266837#include<iostream>#include<cstring>usingnamespace std;constint N =2030;intgcd(int a,int b){return b ?gcd(b, a % b): a;}int dist[N][N];intmain(){memset(dist,0x3f,sizeof dist);for(int i =1; i <=2021; i ++)for(int j =1; j <=2021; j ++){if(i == j) dist[i][j]=0;elseif(abs(i - j)<=21) dist[i][j]=min(dist[i][j], i * j /gcd(i, j));}for(int i =1; i <=2021; i ++)for(int j =1; j <=2021; j ++)for(int k =1; k <=2021; k ++)dist[i][j]=min(dist[i][j], dist[i][k]+ dist[k][j]);cout << dist[1][2021];}
試題F :時間顯示
#include<iostream>#include<cstring>usingnamespace std;typedeflonglong ll;intmain(){ll n; cin >> n;ll time = n /1000;ll ss = time %60;ll mm = time /60%60;ll hh = time /3600%24;printf("%02lld:%02lld:%02lld", hh, mm, ss);}
試題G :砝碼稱重
砝碼可以放在天平兩邊或者不放,相當于砝碼的重量可以為正貢獻也可以為負貢獻或者沒有貢獻
由于j + w[i],因此可能會出現越界,要把M開大一些
#include<iostream>#include<cmath>usingnamespace std;constint N =110, M =2e5+10;int n, w[N];bool f[N][M];intmain(){cin >> n;int sum =0;for(int i =1; i <= n && cin >> w[i]; i ++) sum += w[i];f[0][0]=true;for(int i =1; i <= n; i ++){for(int j =0; j <=100000; j ++){f[i][j]= f[i -1][j];f[i][j]|= f[i -1][abs(j - w[i])];f[i][j]|= f[i -1][j + w[i]];}}int res =0;for(int i =1; i < M; i ++)if(f[n][i])res ++;cout << res;}// TLE#include<iostream>usingnamespace std;constint N =110, M =1e5+10;int n, w[N];bool vis[M];voiddfs(int i,int sum){if(i == n +1){vis[abs(sum)]=true;return;}dfs(i +1, sum);dfs(i +1, sum - w[i]);dfs(i +1, sum + w[i]);}intmain(){cin >> n;for(int i =1; i <= n && cin >> w[i]; i ++);dfs(1,0);int res =0;for(int i =1; i < M; i ++)if(vis[i])res ++;cout << res;}
#include<iostream>usingnamespace std;typedeflonglong ll;int x;ll C(int n,int m){ll res =1;for(int i = n, j =1; i >= n - m +1; i --, j ++){res = res * i / j;if(res > x)return res;}return res;}boolcheck(int k){int l =2* k -1, r =max(l, x)+1;// 保證右邊界大于等于左邊jie// int l = 2 * k, r = x;while(l +1!= r){int mid = l + r >>1;if(C(mid, k)>= x) r = mid;else l = mid;}if(C(r, k)== x){cout <<1ll* r *(r +1)/2+ k +1;returntrue;}returnfalse;}intmain(){cin >> x;for(int i =17;; i --)if(check(i))break;}// 20%分數#include<iostream>#include<cmath>usingnamespace std;int f[2510][2510];int cnt;intmain(){int n; cin >> n;f[0][0]=1;for(int i =1; i <=2500; i ++){for(int j =1; j <= i; j ++){f[i][j]= f[i -1][j -1]+ f[i -1][j];cnt ++;if(f[i][j]== n){cout << cnt;return0;}}}}