3的倍数 或运算构造x(牛客第四场)-- triples I

题意:

给你一个数,希望你能用最少的3的倍数或运算成它,让你输出答案。

思路:

进制%3有规律,1、2、4、8、16%3是1、2、1、2、1 ...

利用这一点分情况取一些位合成一些数就是答案了。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 void swapp(int &a,int &b);
 29 double fabss(double a);
 30 int maxx(int a,int b);
 31 int minn(int a,int b);
 32 int Del_bit_1(int n);
 33 int lowbit(int n);
 34 int abss(int a);
 35 //const long long INF=(1LL<<60);
 36 const double E=2.718281828;
 37 const double PI=acos(-1.0);
 38 const int inf=(1LL<<29);
 39 const double ESP=1e-9;
 40 const int mod=(int)1e9+7;
 41 const int N=(int)1e6+6;
 42  
 43 int _1[100],_2[100];
 44  
 45 int main()
 46 {
 47     int sum=0;
 48     int T;
 49     sc("%d",&T);
 50     while(T--)
 51     {
 52         long long a;
 53         int cnt1=0,cnt2=0;
 54         sc("%lld",&a);
 55         if(a%3==0)
 56         {
 57             pr("1 %lld\n",a);
 58             continue;
 59         }
 60         fr(i,60,0)
 61         {
 62             if((a>>i)&1)
 63             {
 64                 if(i%2==0)
 65                     _1[++cnt1]=i;
 66                 else
 67                     _2[++cnt2]=i;
 68             }
 69         }
 70         int tcnt1=cnt1,tcnt2=cnt2;
 71         long long ans1=0,ans2=0;
 72         int temp=0;
 73         if(cnt1&&cnt2)
 74         {
 75             ans1+=(1LL<<_1[cnt1--]);
 76             ans1+=(1LL<<_2[cnt2--]);
 77             fo(i,1,cnt1)
 78                 ans2+=(1LL<<_1[i]),temp+=1;
 79             fo(i,1,cnt2)
 80                 ans2+=(1LL<<_2[i]),temp+=2;
 81             if(temp%3==1)
 82                 ans2+=(1LL<<_2[tcnt2]);
 83             if(temp%3==2)
 84                 ans2+=(1LL<<_1[tcnt1]);
 85         }
 86         else if(cnt1==0&&cnt2)
 87         {
 88             ans1+=(1LL<<_2[cnt2--]);
 89             ans1+=(1LL<<_2[cnt2--]);
 90             ans1+=(1LL<<_2[cnt2--]);
 91             fo(i,1,cnt2)
 92                 ans2+=(1LL<<_2[i]);
 93             if(ans2%3==1)
 94                 ans2+=(1LL<<_2[tcnt2]);
 95             else if(ans2%3==2)
 96                 ans2+=(1LL<<_2[tcnt2]),ans2+=(1LL<<_2[tcnt2-1]);
 97         }
 98         else if(cnt1&&cnt2==0)
 99         {
100             ans1+=(1LL<<_1[cnt1--]);
101             ans1+=(1LL<<_1[cnt1--]);
102             ans1+=(1LL<<_1[cnt1--]);
103             fo(i,1,cnt1)
104                 ans2+=(1LL<<_1[i]);
105             if(ans2%3==1)
106                 ans2+=(1LL<<_1[tcnt1])+(1LL<<_1[tcnt1-1]);
107             else if(ans2%3==2)
108                 ans2+=(1LL<<_1[tcnt1]);
109         }
110         pr("2 %lld %lld\n",ans1,ans2);
111     }
112     return 0;
113 }
114  
115 /**************************************************************************************/
116  
117 int maxx(int a,int b)
118 {
119     return a>b?a:b;
120 }
121  
122 void swapp(int &a,int &b)
123 {
124     a^=b^=a^=b;
125 }
126  
127 int lowbit(int n)
128 {
129     return n&(-n);
130 }
131  
132 int Del_bit_1(int n)
133 {
134     return n&(n-1);
135 }
136  
137 int abss(int a)
138 {
139     return a>0?a:-a;
140 }
141  
142 double fabss(double a)
143 {
144     return a>0?a:-a;
145 }
146  
147 int minn(int a,int b)
148 {
149     return a<b?a:b;
150 }

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/11437294.html