triples I (bitwise OR operation and characteristics 3) (more than 2019 cattle off summer school camp (fourth) D)

Example 1:

Input:

2

3

7

Output:

1 3
2 3 6

Description: . 3 =. 3, (. 3 |. 6). 7 =

Meaning of the questions: digital output as little as possible, according to their position or results of digital input a.

:( solution to a problem that can not read explanations, see Shandong Gangster code to read it) First of all: There are two strange phenomenon, is a binary digit (my definition is the first 0, that is an even number) and only 2 1, and respectively at the even bit and odd bit, this number must be a multiple of 3. If a number a (a> 3) at the even bit and odd bit are 1, and the number 1 is greater than or equal to 1, as a% 3 == 1, only a take on even bit 1 to 3% == 0; when a% 3 == 2, took only 1% to 3 == 0 on an odd bit.

1, if the input is a multiple of 3, then a direct output;

2, if the input is not a multiple of three, the number of recording a binary number and position on the odd bit and the even bit is 1, then the following actions:

If the odd bit and the even bit 1 are present, the builder num1 = 0, num2 = a, and if a% 3 == 1, then on a 1 num2 away even bit, odd bit and num1 to respective even bit an existing 1; if a% 3 == 2, will be a 1 on the odd bit away num2, num1 to each even bit and odd bit an existing one;

If there is only one position on the odd or even bit, similarly configured num1 = 0, num2 = a, and progressively take 1 to existing num1 num2 1 on, until num2% 3 == 0, num2 stop away 1 , continued existing num1 1 until the end of num1% 3 = 0, complete the construction.

AC Code:

 

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define pb push_back
 4 using namespace std;
 5 LL num1,num2;
 6 void solve(LL x)
 7 {
 8     num1=0,num2=x;
 9     vector<int>odd,even;
10     for(int i=0;i<=63;i++){//将奇数位和偶数位上的1的位置取出并保存
11         if((x>>i)&1){
12             if(i%2)odd.pb(i);
13             else even.pb(i);
14         }
15     }
16     if(odd.size()&&even.size()){//奇数位和偶数位上都存在1的情况
17         if(x%3==1){
18             num2^=(1LL<<even[0]);
19             num1^=(1LL<<even[0]);
20             num1^=(1LL<<odd[0]);
21         }
22         else{
23             num2^=(1LL<<odd[0]);
24             num1^=(1LL<<even[0]);
25             num1^=(1LL<<odd[0]);
26         }
27     }
28     else if(odd.size()){//只有奇数位上存在1的情况
29         int i;
30         for(i=0;i<odd.size();i++){
31             num1^=(1LL<<odd[i]),num2^=(1LL<<odd[i]);
32             if(num2%3==0){
33                 i++;break;
34             }
35         }
36         for(;i<odd.size();i++){
37             if(num1%3==0)break;
38             num1^=(1LL<<odd[i]);
39         }
40     }
41     else{//只有偶数位上存在1的情况
42         int i;
43         for(i=0;i<even.size();i++){
44             num1^=(1LL<<even[i]),num2^=(1LL<<even[i]);
45             if(num2%3==0){
46                 i++;break;
47             }
48         }
49         for(;i<even.size();i++){
50             if(num1%3==0)break;
51             num1^=(1LL<<even[i]);
52         }
53     }
54 }
55 int main()
56 {
57     LL t,n;
58     scanf("%lld",&t);
59     while(t--){
60         scanf("%lld",&n);
61         vector<LL>num;
62         if(n%3==0){
63             num.pb(n);
64         }
65         else {
66             solve(n);//a%3!=0的情况下进行构造。
67             num.pb(num1);num.pb(num2);
68         }
69         if(num.size()==1)
70             printf("1 %lld\n",num[0]);
71         else if(num.size()==2)
72             printf("2 %lld %lld\n",num[0],num[1]);
73     }
74     return 0;
75 }

 

Guess you like

Origin www.cnblogs.com/Aamir-Dan/p/11263280.html