zoukankan      html  css  js  c++  java
  • Codeforces Round #554 (Div. 2) 1152B. Neko Performs Cat Furrier Transform

    学了这么久,来打一次CF看看自己学的怎么样吧

    too young too simple

    1152B. Neko Performs Cat Furrier Transform

    题目链接:"https://codeforces.com/contest/1152/problem/B"

    题目大意:将数字经过一系列的处理 (轮流进行加法操作和异或操作且加数和异或数有要求,加法只能加一,异或只能对2的n次方-1异或)( + ^ + ^ + ^ + ^ ......) 后变成——在二进制表示下从最高位到最低位都是1。比如

           39     →   56   →   57    →   62   →   63
        0010 0111→0011 1000→0011 1001→0011 1110→0011 1111
    

    题目思路:从最高位开始把0变成1(因为对更小的数进行异或操作只会对低位影响而不会对高位有影响)

    代码如下

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {	
    	int x,xx,op=0,j=0,opp=0;
    	int ans[50]={0};
    	cin>>x;
    	while(1)
    	{
    		xx=x;
    		int sum=0,man=0;
    		for(int i=0;xx!=1;i++){
    			if(xx%2==1)man++;
    			xx>>=1;
    			sum++;
    		}
    		
    		if(sum==man)break;
    		
    		xx=x;
    		for(int i=0;xx!=1;i++){
    			if(xx%2==0)op=i;
    			xx>>=1;
    		}
    		int er=1;
    		for(int i=0;i<=op;i++)er*=2;
    		x=x^(er-1);
    		opp++;
    		ans[j++]=op;
    		xx=x;
    		man=0;
    		sum=0;
    		for(int i=0;xx!=1;i++){
    			if(xx%2==1)man++;
    			xx>>=1;
    			sum++;
    		}
    		if(sum==man)break;
    		x=x+1;
    		opp++;
    	}
    	printf("%d
    ",opp);
    	for(int i=0;i<j;i++)printf("%d ",ans[i]+1);
    	return 0;
    } 
    
  • 相关阅读:
    洛谷P1070 道路游戏
    洛谷P1556 幸福的路
    洛谷P1457 城堡 The Castle
    洛谷P1298 最接近的分数
    2017-9-13 NOIP模拟赛[xxy]
    洛谷P3405 [USACO16DEC]Cities and States省市
    洛谷P1549 棋盘问题(2)
    洛谷P1578 奶牛浴场
    洛谷P2073 送花
    洛谷P3797 妖梦斩木棒
  • 原文地址:https://www.cnblogs.com/--ChenShou--/p/10778678.html
Copyright © 2011-2022 走看看