目录
我们N个真是太厉害了
思路:
代码:
折返跑
思路:
代码:
好好好数
思路:
代码:
魔法之森的蘑菇
思路:
代码:
我们N个真是太厉害了
思路:
我们先给数组排序,如果最小的元素不为1,那么肯定是吹牛的,我们拿一个变量记录前缀和,如果当前元素大于它前面所有元素的和+1,那么sum+1是不能到达的值。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
#define pb push_back
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;void solve()
{int n;cin >> n;vector<int>a(n + 1);for (int i = 1; i <= n; i++) cin >> a[i];sort(a.begin() + 1, a.end());if (a[1] != 1) {cout << 1 << '\n';return;}int sum = 1;for (int i = 2; i <= n; i++) {if (a[i] > sum + 1) {cout << sum + 1 << '\n';return;}sum += a[i];if (sum >= n) {cout << "Cool!\n";return;}}cout << "Cool!\n";}
signed main() {ios; TESTsolve();return 0;
}
折返跑
思路:
其实是一个组合数学的题目,我们只要规定每个折返至少挪一米,那么剩下的就可以随便安排,就是一个C(可用的距离,折返次数)就是答案,我们可以预处理一下1到1e6的阶乘。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
#define pb push_back
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;int f[N], a[N], sum[N];
int C(int n, int m) {if (n < m) return 0;if (m == 0) return 1;return f[n] * qpow(f[n - m] * f[m], mod - 2, mod) % mod;
}
void solve()
{int n, m;cin >> n >> m;cout << C(n - 2, m - 1) << '\n';}
signed main() {f[0] = 1;f[1] = 1;for (int i = 2; i <= 1e6; i++) f[i] = f[i - 1] * i % mod;ios; TESTsolve();return 0;
}
好好好数
思路:
直接将n变成k进制数,发现答案就是最大的因为,当k为1时,直接输出1。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
#define pb push_back
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 3e5 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 998244353;
using namespace std;vector<int>f;
int n, k;
void solve()
{cin >> n >> k;if (k == 1) {cout << 1 << '\n';return;}int ans = 1;while (n) {ans = max(ans, n % k);n /= k;}cout << ans << '\n';}signed main() {ios; TESTsolve();return 0;
}
魔法之森的蘑菇
思路:
这里我用了SPFA算法,因为小红必须遇到‘*’时才能有转向的机会,提到转向,那么则证明图上不止有位置这个状态,还有方向,所以我们可以定义三维vis和dis数组,因为多了一个方向,然后bfs搜即可,最后我们查看目标位置的四个方向的最小值,如果这个最小值与初始值一样,那么则证明没有办法到达,直接输出-1。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#define int long long
#define pb push_back
#define all(x) x.begin(), (x).end()
#define TEST int T; cin >> T; while (T--)
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define lowbit(x) x&(-x)
#define pll pair<int,int>
const int N = 1e6 + 30;
const int M = 1e3 + 10;
const int inf = 512785182741247112;
const int mod = 1e9 + 7;
using namespace std;
char mp[1001][1001];
int dir[][2] = { {-1,0},{0,1},{1,0},{0,-1} };
void solve() {int n, m;cin >> n >> m;int sx, sy, ex, ey;for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) {cin >> mp[i][j];if (mp[i][j] == 'S') {sx = i, sy = j;}if (mp[i][j] == 'T') {ex = i, ey = j;}}}vector<vector<vector<int>>>dis(n+1,vector<vector<int>>(m+1,vector<int>(4,inf)));vector<vector<vector<bool>>>vis(n + 1, vector<vector<bool>>(m + 1, vector<bool>(4, false)));queue<tuple<int,int,int>>q;for (int i = 0; i < 4; i++) {//U,R,D,Lq.emplace(i, sx, sy);dis[sx][sy][i] = 0;vis[sx][sy][i] = true;}while (q.size()) {int dr, x, y;tie(dr, x, y) = q.front();q.pop();vis[x][y][dr] = false;if (mp[x][y] == '*') {//(dr + 1) % 4 向右转 (dr-1+4) % 4 向左转for (int i = 1; i <= 3; i += 2) {if (dis[x][y][(dr + i) % 4] > dis[x][y][dr]) {dis[x][y][(dr + i) % 4] = dis[x][y][dr];if (vis[x][y][(dr + i) % 4]) continue;q.emplace((dr + i) % 4, x, y);vis[x][y][(dr + i) % 4] = true;}}}int tx = x + dir[dr][0];int ty = y + dir[dr][1];if (tx<1 || ty<1 || tx>n || ty>m) continue;if (vis[tx][ty][dr]) continue;if (dis[tx][ty][dr] > dis[x][y][dr] + 1) {dis[tx][ty][dr] = dis[x][y][dr] + 1;if (vis[tx][ty][dr]) continue;if (mp[tx][ty] == '#') continue;q.emplace(dr, tx, ty);vis[tx][ty][dr] = true;}}int ans = inf;for (int i = 0; i < 4; i++) {ans = min(ans, dis[ex][ey][i]);}if (ans == inf) cout << -1 << '\n';else cout << ans << '\n';
}signed main() {ios; TESTsolve();return 0;
}