2021-03-30 CodeCraft-21 and Codeforces Round #711 (Div. 2)

这场比赛,只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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值