这场比赛,只A了一道,暴露了我很多不足的地方。
A. GCD Sum
题意:就是判断一个数和他所有位上的数加起来的值的gcd是否大于一,等于一就对当前数加1,直到两者的gcd大于一。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ll gcd(ll a, ll b)
{
if (b == 0) return a;
return gcd(b, a % b);
}
ll sum (ll n1)
{
ll x = 0;
while (n1)
{
x += n1 % 10;
n1 /= 10;
}
return x;
}
int main()
{
int t;
ll n, y;
cin >> t;
while (t -- )
{
cin >> n;
ll n2 = n;
y = gcd(n2, sum(n2));
while (y == 1)
{
n2 ++;
y = gcd(n2, sum(n2));
}
cout << n2 << endl;
}
return 0;
}
B. Box Fitting
题意:将高度为1,宽度为2的幂的方块放入容器中,求最小的高度。
贪心准则;将能放在容器中的最大的方块先放在容器中。
multiset能存储有序且重复的元素。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 100010;
int t, n, W,w;
multiset<int> st;
int main()
{
cin >> t;
while (t -- )
{
cin >> n >> W;
for (int i = 1; i <= n; i ++ )
{
scanf("%d", &w);
st.insert(w);
}
int height = 1, space_left = W;
while (!st.empty())
{
auto it = st.upper_bound(space_left);
if (it != st.begin())
{
it = prev(it);
space_left -= *it;
st.erase(it);
}
else
{
height ++;
space_left = W;
}
}
cout << height << endl;
}
return 0;
}
C. Planar Reflections
题意;一个衰变期为k的粒子穿过一个个屏障,每个屏障往反方向发射一个衰变期为k - 1的粒子。
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1010;
typedef long long ll;
int t, n, k;
int save[N], save2[N];
int main()
{
cin >> t;
while (t -- )
{
cin >> n >> k;
ll ans = 1;
for (int i = 1; i <= n; i ++ ) save[i] = 1;
for (int i = 1; i <= k - 1; i ++ )
{
ll pre = 0;
for (int j = 1; j <= n; j ++ )
{
ans += save[j];
ans %= MOD;
save2[j] = pre;
pre += save[j];
pre %= MOD;
}
for (int i = 1; i <= n; i ++ ) save[i] = save2[i];
reverse(save + 1, save + n + 1);
}
cout << ans << endl;
}
return 0;
}