A.Sliding
思路:
编号为(r - 1) * m + c 的数,从它后面的数开始移动,
因此需要移动的球:总数 - (r - 1) * m + c,、
并且除了每一行的第一个元素需要移动m个位置,其他点移动1个格子即可。
C++代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
int T,n,m,r,c;
void solve(){
cin >> n >> m >> r >> c;
int last = n * m - ((r - 1) * m + c);
int ans = (last - (n - r)) + (n - r) * m;
cout << ans <<"\n";
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
B.Everyone Loves Tres
思路:
33和66的最小公倍数为66,枚举66的倍数,
(1) n == 1 或者 n == 3 时 -1;
(2) n为偶数时最后两位“66”
(3) n为奇数时最后四位“6366”
C++代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
int T,n;
void solve(){
cin >> n;
if(n == 1 || n == 3){
cout << "-1\n";
return;
}
string s(n - 2,'3');
s += "66";
if(n & 1) //如果是奇数
s[n - 4] = '6';
cout << s << "\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
C. Alya and Permutation
算法:构造
思路:
当 n 为奇数时,k 的最大值为 n;
可以构造3 , 1 , n − 1 , n 这样的序列放在最末尾。
当 n 为偶数时,k的最大值为 2^(x+1) − 1,
我们可以构造3, 1, 2^x - 2, 2^x-1, n 这样的序列放在最末尾,
其中 x 为二进制下 n 最高位的 1 所在的位数。
C++代码
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int T,n;
void solve(){
cin >> n;
if(n == 6){
cout << 7 << endl;
cout <<"1 2 4 6 5 3 "<< endl;
return;
}
if(n % 2 == 1) //如果 n 为奇数
{
cout << n << endl;
cout << "2 ";
for(int i = 4; i <= n - 2; i ++){
cout << i << " ";
}
//核心部分
cout << "3 1 " << n - 1 << " " << n << endl;
}
else {
int x = __lg(n); //取出 n的二进制中最高位的 1
cout <<(int) pow(2,x + 1) - 1 << endl; //我们会把所有二进制数为全部填为 1
int y = (int) pow(2,x) - 1; //比 n 小的全为1的二进制数
cout << "2 ";
for(int i = 4; i <= n - 1; i ++){
if(i != y && i != y - 1){
cout << i << " ";
}
}
cout << "3 1 " << y - 1 << " " << y << " " << n << endl;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}