CodeForces - 1152B二進制+思維

【題目鏈接】Neko Performs Cat Furrier Transform
【題目分析】要求將一個數字變成2n-1,通過嘗試我們發現如果將最低位的全零位和對應的全一數字(例如11000對應的就是111)異或那么數字就會變成想要的結果(11111)
但是如果前面還有0(比如110100)那么過程應該如下:110100^000011變成110111加一后變成111000然后就會得到結果,總之我們需要做的就是不斷將末尾的全零變成全1,然后在加一的時候就會進位將前面的0消滅
為了檢驗是否已經得到結果,我們不妨預處理一個所有結果的數組,然后在數組中用lower_bound和upper_bound找
尾部的全零位的查找可以借鑒樹狀數組中最低位1lowbit(x)=x&(-x),最低位1后面全都是0,因此全零對應的的全一數字為lowbit(x)-1

#include<cstdio>
#include<algorithm>
using namespace std;int s[50];
int cnt=0;
int check[30];bool find(int x)
{//printf("test : lower_bound(check,check+30,x)-check=%d\n",lower_bound(check,check+30,x)-check);//printf("test : upper_bound(check,check+30,x)-check=%d\n",upper_bound(check,check+30,x)-check);if(lower_bound(check,check+30,x)-check==upper_bound(check,check+30,x)-check)return false;elsereturn true;
}
int work(int x)
{return (x&(-x))-1;
}
int main()
{for(int i=0;i<30;i++){check[i]=(1<<i)-1;}int x,y,ans=0;scanf("%d",&x);while(ans<40){//printf("test x=%d\n",x);y=work(x);//printf("test y=%d\n",y);s[cnt++]=lower_bound(check,check+30,y)-check;x=x^y;//printf("test x=%d\n",x);ans++;if(!find(x)){//printf("test x=%d\n",x);x++;ans++;}else{break;}}printf("%d\n",ans);for(int i=0;i<cnt;i++){printf("%d ",s[i]);}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384178.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384178.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384178.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C語言文件操作之fgets()

http://blog.csdn.net/daiyutage/article/details/8540932 來說一說fgets(..)函數。 原型 char * fgets(char * s, int n,FILE *stream); 參數&#xff1a; s: 字符型指針&#xff0c;指向存儲讀入數據的緩沖區的地址。 n: 從流中讀入n-1個字符 stream &#xff1a; 指向讀取…

指針與零的比較以及浮點型與零的比較

指針和零的比較 int *p null;if(p ! null){p 20; } 整形和零的比較 int i 0; if(0i) {... } 浮點型和零的比較 判斷一個浮點數是不是零 #define EXP 0.0000000000001 float f 0.00001; if((f > -EXP)&&(f < EXP)) {... } 擴展后 判斷一個浮點數是不…

CodeForces 1138B暴力+剪枝

【題目鏈接】Circus 【題目分析】理解題意以后發現并沒有什么思路&#xff0c;沒有什么算法能用&#xff0c;這個時候就應該想到計算機解題的本質——暴力求解。相應的就要想到剪枝的條件&#xff0c;肯定不能盲目的暴力求解。 總共有四種人&#xff1a;00,01,10,11&#xff0c…

MYSQL錯誤代碼#1045 Access denied for user 'root'@'localhost'

http://blog.csdn.net/lykezhan/article/details/70880845 遇到MYSQL“錯誤代碼#1045 Access denied for user rootlocalhost (using password:YES)” 需要重置root賬號權限密碼&#xff0c;這個一般還真不好解決。 不過&#xff0c;這幾天調試的時候真的遇到了這種問題&#x…

C語言操作符

移位表達式 左移操作符<< 左邊拋棄,右邊補零 右移操作符>> 1.邏輯右移 左邊補零,右邊丟棄 2.算數右移 左邊補符號位,右邊丟棄 注意: 1.左移一位相當于乘2,右移一位相當于除2,并且在內存中存放的是二進制的補碼,且移位操作符只對int型數操作 2.移位操作符不要移動…

棋盤問題——DFS

【題目描述】 在一個給定形狀的棋盤&#xff08;形狀可能是不規則的&#xff09;上面擺放棋子&#xff0c;棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列&#xff0c;請編程求解對于給定形狀和大小的棋盤&#xff0c;擺放k個棋子的所有可行的擺放方…

linux安裝mysql和使用c語言操作數據庫的方法 c語言連接mysql

http://www.jb51.net/article/46139.htm 1. MySQL的安裝與配置&#xff1a; 在Ubuntu下安裝MySQL方法很簡單&#xff0c;使用如下命令&#xff1a; 復制代碼 代碼如下:sudo apt-get install mysql-server安裝的過程中系統會提示設置root密碼&#xff0c;此過程可以跳過&#…

常量變量以及循環

常量 1.三目運算詞 三字母詞表達字符???([??)]??<{??>} 2.循環 1).數組元素以及變量在內存中的分配順序 2)goto語句應用 //電腦關機程序 #include<stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> int ma…

Dungeon Master——BFS

【題目描述】 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move …

Linux 環境 C語言 操作MySql 的接口范例

http://www.cnblogs.com/wunaozai/p/3876134.html 接上一小節&#xff0c;本來是計劃這一節用來講數據庫的增刪改查&#xff0c;但是在實現的過程中&#xff0c;出現了一點小問題&#xff0c;也不是技術的問題&#xff0c;就是在字符界面上比較不好操作。比如要注冊一個帳號&a…

二進制邏輯運算符有關練習題

//1.寫一個函數返回參數二進制中 1 的個數 #include<stdio.h> int div 0; //除數 int rem 0; //余數 int count 0; //計1 int count_one_bits(unsigned int div) {int con 0; //商while (div > 1){con div / 2;rem div % 2;div con;if (1 rem){count;}}…

Catch That Cow——BFS

【題目描述】 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo…

利用mysql提供的c語言接口操作數據庫

http://blog.csdn.net/bladeandmaster88/article/details/52980872 //1.工程要在c/c->常規->附加包含目錄添加mysql.h的路徑D:\mysql5.5\include //2.工程要在鏈接器->常規->附加庫目錄添加libmysql.lib的路徑D:\mysql5.5\lib #include <WinSock2.h>/…

數組相關運算

數組的初始化 數組及指針在內存中的存儲 一維數組在內存中的存儲 有關數組的運算 //一維數組 int a[] {1,2,3,4}; printf("%d\n",sizeof(a));//16這里的a表示的是整個數組,計算出的是整個數組的大小,單位為byte printf("%d\n",sizeof(a 0));/*a沒有單獨…

Find The Multiple——簡單搜索+大膽嘗試

【題目描述】 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more t…

C語言操作MYSQL小例子

http://blog.csdn.net/small_qch/article/details/8180678 初學使用用C語言操作MYSQL&#xff0c;寫了個小例子&#xff0c;帖上來獻丟人一下&#xff0c;呵呵。 程序很簡單&#xff0c;先連接數據庫&#xff0c;然后向class1表中插入一條數據&#xff0c;最后獲取并輸出整個cl…

Find a way——BFS

【題目描述】 Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the ce…

用C語言操作MySQL數據庫

http://blog.chinaunix.net/uid-26743670-id-3479887.html 參考MYSQL的幫助文檔整理 這里歸納了C API可使用的函數&#xff0c;并在下一節詳細介紹了它們。請參見25.2.3節&#xff0c;“C API函數描述”。 函數 描述 mysql_affected_rows() 返回上次UPDATE、DELETE或INSERT查…

三字棋

整個游戲可以分為以下幾個環節 1.打印一個玩游戲的菜單 2.玩游戲 (1)玩家走一步 (2)電腦走一步 每走一步對結果進行顯示,其中游戲的結果可分為玩家贏,電腦贏,以及平局 代碼顯示如下 game.c #define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<stdio.h&…

gets fgets 區別

http://www.cnblogs.com/aexin/p/3908003.html 1. gets與fgets gets函數原型&#xff1a;char*gets(char*buffer);//讀取字符到數組&#xff1a;gets(str);str為數組名。 gets函數功能&#xff1a;從鍵盤上輸入字符&#xff0c;直至接受到換行符或EOF時停止&#xff0c;并將讀取…