A. Reverse and Concatenate
题解
通过几次简单操作后,我们就可以得知,除了k = 0和字符串s本身对称的情况下,答案是1,其余都是2
代码
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int T, len, k;
string s;
int main ()
{
cin >> T;
while (T -- )
{
cin >> len >> k >> s;
string t = s;
reverse (t.begin(), t.end());
if (s == t || k == 0) cout << "1" << endl;
else cout << "2" << endl;
}
return 0;
}
C. OKEA
题解
大致意思就是:
有n个架子,每个架子上有k件物品,物品价值由1~n * k。
现在要求我们让每一个架子上的数的所有子集的平均值都是整数,问是否能够达成,YES OR NO?
首先,我们可以明确的一点就是,当k = 1的时候成立。
那么,当k > 1的时候,情况是否有所变化,
列举k = 2,我们会发现在此情况下,n必须是偶数才能成立,
同样,列举k = 3,我们同样发现,n必须是偶数,
易知,n必须是偶数,否则输出NO
又由观察得出,{1, 3, 5, 7, … } 这个数组的所有子集平均值都是整数,
这时,我们重新考虑,当k > 1时,是要n是偶数,我们总能有办法得出相应的数组,
所以结果出来了。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define endl '\n'
void solve ()
{
int n, k; cin >> n >> k;
if (k == 1){
cout << "YES" << endl;
for (int i = 1; i <= n * k; i ++ ) cout << i << endl;
return ;
}
if (n % 2 != 0){
cout << "NO" << endl;
return ;
}
cout << "YES" << endl;
int t = 0;
for (int i = 1; i <= n * k; i += 2 )
{
cout << i << " ";
t ++;
if (t % k == 0) cout << endl;
}
for (int i = 2; i <= n * k; i += 2 )
{
cout << i << " ";
t ++;
if (t % 2 == 0) cout << endl;
}
}
int main ()
{
#ifdef LOCAL
freopen ("data.in", "r", stdin);
freopen ("data.out", "w", stdout);
#endif
ios :: sync_with_stdio(false);
int T; cin >> T;
while (T -- ) solve();
return 0;
}