2019 cattle off more school fourth question D
The meaning of problems
Give you a \ (the n-\) , or ask at least a few number of operations it can be equal to \ (the n-\) , and the number of outputs and the few numbers. Subject to the described \ (n-\) must meet the conditions (not output \ (n = 1 \) or the like does not exist).
Thinking
We can know playing table may be formed of at least n \ (1 \) one or a \ (2 \) or the number of them.
- First, we pre-judge \ (n \% 3 == 0 \) This output \ (n \) itself on it
The number may be made of other \ (2 \) for ORing number.
- The \ (n-\) into binary, each bit 1 to be extracted into the collection \ (R & lt \)
- 找到两个集合\(a、b\),\(a\cup b = R\)
\(num1 = \sum_{i=0}^{a.size()} x_{i} (x_{i}\in a)\)
\(num1 \% 3 == 0\)
\(num2 = \sum_{i=0}^{b.size()} y_{i} (y_{i}\in b)\)
\(num2 \% 3 == 0\) - And then were put (a, b \) \ the collection of a few add up to answer.
Sample (WA to solve two examples cry)
2
85
682
(1010101、1010101010)上面两个样例的二进制
AC Code
#include<bits/stdc++.h>
#define mes(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
int a[100];
int ans[3];
void solve(ll x){ //转换n为二进制
int i = 0;
while(x){
a[i++] = x%2;
x /= 2;
}
}
int main(){
int t;
ll n;
scanf("%lld", &t);
while(t--){
scanf("%lld", &n);
if(n % 3ll == 0){
printf("1 %lld\n", n);
continue;
}
mes(a, 0);
solve(n);
ans[1] = ans[2] = 0; //分别计算二进制位为1的数字取模3分别为1和2的数量
for(int i = 0; i <= 64; i++){
if(a[i] == 1){
int num = (1ll<<i)%3ll;
ans[num]++;
}
}
int sum = ans[1] + ans[2]*2;
int b[3][3];
mes(b, 0);
if(sum % 3 ==1){ //强行分组(本人比较菜只会这样写)
if(ans[1] == 0){
b[1][1] = 0;
b[1][2] = ans[2] - 2;
b[2][1] = 0;
b[2][2] = 3;
}
else{
b[1][1] = ans[1]-1;
b[1][2] = ans[2];
if(ans[2] == 0){
b[2][1] = 3;
}
else{
b[2][1] = 1;
b[2][2] = 1;
}
}
}
else if(sum %3 == 2){
if(ans[2] == 0){
b[1][1] = ans[1]-2;
b[1][2] = 0;
b[2][1] = 3;
b[2][2] = 0;
}
else{
b[1][1] = ans[1];
b[1][2] = ans[2]-1;
if(ans[1] == 0){
b[2][1] = 0;
b[2][2] = 3;
}
else{
b[2][1] = 1;
b[2][2] = 1;
}
}
}
ll num1 = 0, num2 = 0;
for(int i = 0; i < 64; i++){
if(a[i] == 1){
int num = (1ll<<i)%3;
if(b[2][num] == ans[num]){
num2 += (1ll<<i);
b[2][num]--;
}
if(b[1][num] > 0){
num1 += (1ll<<i);
b[1][num]--;
}
ans[num]--;
}
}
printf("2 %lld %lld\n", num1, num2);
}
return 0;
}