@[toc](CodeCraft-21 and Codeforces Round #711 (Div. 2))
A [GCD Sum]
模拟吧。
/*
*@author SunLakeWalk
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <limits.h>
#include <sstream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inlin")
using namespace std;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 100010, INF = 0x3f3f3f3f, mod = 1e9 + 7, base = 131;
const double eps = 1e-6, PI = acos(-1);
LL n;
void work()
{
cin >> n;
for (LL i = n; ; i ++ )
{
string s = to_string(i);
int sum = 0;
for (int j = 0; j < s.size(); j ++ )
sum += s[j] - '0';
if (__gcd(i, (LL)sum) > 1)
{
cout << i << endl;
return;
}
}
}
int main()
{
//ios;
int T = 1;
cin >> T;
while (T -- )
{
work();
}
return 0;
}
B [Box Fitting]
贪心思想
法一:
凑2,从大到小来吧,反正都是2的质数级别,大的至少是小的2被,不用担心用一个大的占了两个小的空间
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 110;
int n, w;
int a[N];
void work()
{
memset(a, 0, sizeof a);
cin >> n >> w;
int x;
for (int i = 0; i < n; i ++ )
{
cin >> x;
int t = __builtin_ctz(x);//返回二进制下末尾0的个数
a[t] ++;
}
int cnt = 0;
while (n)
{
int m = w;
for (int i = 20; i >= 0; i -- )
{
while (m >= (1 << i) && a[i])
{
a[i] --;
m -= (1 << i);
n --;
}
}
cnt ++;
}
cout << cnt << endl;
}
int main()
{
int T; cin >> T;
while (T -- )
{
work();
}
return 0;
}
法二
直接单调队列,贪心来做嘛。
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, w;
int a[N];
void work()
{
cin >> n >> w;
for (int i = 0; i < n; i ++ ) cin >> a[i];
sort(a, a + n, greater<int>());
priority_queue<int> q;
q.push(w - a[0]);
for (int i = 1; i < n; i ++ )
{
int t = q.top();
if (t >= a[i])
{
q.pop();
q.push(t - a[i]);
}
else
{
q.push(w - a[i]);
}
}
cout << q.size() << endl;
}
int main()
{
int T; cin >> T;
while (T -- )
{
work();
}
return 0;
}