AtCoder Beginner Contest 400
A - ABC400 Party
题意
思路
模拟
代码
点击查看代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;
const int mxn = 2e5 + 10;
void solve()
{
int n;
cin >> n;
if (400 % n)
{
cout << -1 << endl;
return;
}
cout << 400 / n << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int __ = 1;
//cin >> __;
while (__--)
{
solve();
}
return 0;
}
B - Sum of Geometric Series
题意
思路
模拟
代码
点击查看代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;
const int mxn = 2e5 + 10;
void solve()
{
int n, m;
cin >> n >> m;
int ans = 0;
for (int i = 0; i <= m; i++)
{
ans += pow(n, i);
if (ans > 1e9)
{
cout << "inf" << endl;
return;
}
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int __ = 1;
//cin >> __;
while (__--)
{
solve();
}
return 0;
}
C - 2^a b^2
题意
思路
将问题 \(1≤2^a×b^2≤n\) 转化为 \(1\le b \le \sqrt {\frac n {2^a}}\),枚举 \(a\) 即可
代码
点击查看代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;
const int mxn = 2e5 + 10;
void solve()
{
int n, ans = 0;
cin >> n;
for (int i = 1; (1LL << i) <= n; i++)
{
int a = (1LL << i);
int b = sqrtl(n / a);
ans += ((b + 1) >> 1);
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int __ = 1;
//cin >> __;
while (__--)
{
solve();
}
return 0;
}
D - Takahashi the Wall Breaker
题意
思路
当成最短路做,\(dis\) 是前踢的次数
注意前踢的范围是\(2\)(写的时候被卡出💩了)
代码
点击查看代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;
const int mxn = 2e5 + 10;
struct node
{
int x, y, k;
node(int x, int y, int k)
{
this->x = x;
this->y = y;
this->k = k;
}
bool operator < (const node& a) const
{
return k > a.k;
}
};
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void solve()
{
int h, w, a, b, c, d;
cin >> h >> w;
vector<vector<char>> g(h, vector<char>(w));
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> g[i][j];
}
}
cin >> a >> b >> c >> d;
a--, b--, c--, d--;
vector<vector<int>> dis(h, vector<int>(w, LLONG_MAX));
vector<vector<bool>> vis(h, vector<bool>(w, false));
priority_queue<node> q;
dis[a][b] = 0;
q.push(node(a, b, dis[a][b]));
while (q.size())
{
auto [x, y, k] = q.top();
q.pop();
if (vis[x][y]) continue;
vis[x][y] = true;
for (int i = 0; i < 4; i++)
{
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 0 || ty < 0 || tx >= h || ty >= w) continue;
if (g[tx][ty] == '.')
{
if (dis[tx][ty] > k)
{
dis[tx][ty] = k;
q.push(node(tx, ty, dis[tx][ty]));
}
}
else
{
if (dis[tx][ty] > k + 1)
{
dis[tx][ty] = k + 1;
q.push(node(tx, ty, dis[tx][ty]));
}
tx += dx[i], ty += dy[i];
if (tx < 0 || ty < 0 || tx >= h || ty >= w || g[tx][ty] == '.') continue;
if (dis[tx][ty] > k + 1)
{
dis[tx][ty] = k + 1;
q.push(node(tx, ty, dis[tx][ty]));
}
}
}
}
cout << dis[c][d] << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int __ = 1;
//cin >> __;
while (__--)
{
solve();
}
return 0;
}
E - Ringo's Favorite Numbers 3
题意
思路
由于 \(A\) 是平方数,那数据范围就缩小到了 \(1e6\),找出范围内每个数的不同质因数个数,并挑出恰有 \(2\) 个质因数的。 要使 \(400数\) 最大,那\(k\)就要最小—— \(2\)。最后只需要找第一个小于 \(A\) 的即可。
代码
点击查看代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int, int> pii;
const int mxn = 1e6 + 10;
vector<int> cnt, num;
void init()
{
cnt.assign(mxn, 0);
for (int i = 2; i < mxn; i++)
{
if (cnt[i]) continue;
for (int j = i; j < mxn; j += i)
{
cnt[j]++;
}
}
for (int i = 2; i < mxn; i++)
{
if (cnt[i] == 2)
{
num.push_back(i * i);
}
}
}
void solve()
{
int a;
cin >> a;
cout << *prev(upper_bound(num.begin(), num.end(), a)) << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int __ = 1;
cin >> __;
init();
while (__--)
{
solve();
}
return 0;
}






浙公网安备 33010602011771号