日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)

發(fā)布時(shí)間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

Little penguin Polo likes permutations. But most of all he likes permutations of integers from?0?to?n, inclusive.

For permutation?p?=?p0,?p1,?...,?pn, Polo has defined its beauty — number?.

Expression??means applying the operation of bitwise excluding "OR" to numbers?xand?y. This operation exists in all modern programming languages, for example, in language?C++?and?Java?it is represented as "^" and in?Pascal?— as "xor".

Help him find among all permutations of integers from?0?to?n?the permutation with the maximum beauty.

Input

The single line contains a positive integer?n?(1?≤?n?≤?106).

Output

In the first line print integer?m?the maximum possible beauty. In the second line print any permutation of integers from?0?to?n?with the beauty equal to?m.

If there are several suitable permutations, you are allowed to print any of them.

Examples

Input

4

Output

20 0 2 1 4 3

題目大意:

? ?給n個(gè)數(shù),分成兩組A和B,A中的可以找B中的連線,求最優(yōu)匹配,權(quán)值為二者的異或值。(

解題報(bào)告:

? 我把題目大意搞得跟二分圖一樣,,,但是其實(shí)不是的。。只是方便理解(看數(shù)據(jù)量也知道不是)

? 可以證明,一定對(duì)于每一個(gè)數(shù)都找到一個(gè)對(duì)應(yīng)的匹配

Since we need to maximize the result, we need to find such permutation, for which the least number of bit disappear. (We consider bit disappeared if it was 1 both in?i?and?pi, so in??it is 0). It turns out that for each?n?there is such permutation that no bit disappear. How to build it? We will be solving problem by iterations while?n?>?0. On each iteration, we need to find the biggest (the leftmost in binary representation) bit which is not?0?in binary representation of?n?and denote it position (bits are numbered from 0) by?b. Now we need to find integer?m?— minimal integer from?0?to?n, inclusive, such that?b-th bit is also?1?in it. After that you can see (look image below), that at??no bit disappear, at??no bit disappear, ..., at??no bit disappear. So, it is good to assign exactly that integers to our permutation, i. e.?pm?=?m?-?1?and?pm?-?1?=?m,?pm?+?1?=?m?-?2?and?pm?-?2?=?m?+?1?and so on. After that assign value?m?-?(n?-?m?+?1)?-?1?to?n?and go to next iteration.

Now when we know how to build permutation and that no bit disappear, the value of the answer is equal to?.

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long using namespace std; const int MAX=1e6 + 5; int ans[MAX]; ll sum=0,x=1; int main() {int n;cin >>n;while(x<=n) x<<=1;x--; for(int i=n; i>=0; i--) {if(ans[i]!=0) continue;while((x^i)>n||ans[x^i]!=0) x>>=1;ans[x^i]=i;ans[i]=x^i;}sum=0;for(int i=0; i<=n; i++)sum+=i^ans[i];cout <<sum<<endl;for(int i=0; i<=n; i++) {printf("%d%c",ans[i],i == n ? '\n' : ' ');}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 波多野结衣精品在线 | 欧美经典一区二区三区 | 玖玖在线视频 | 91传媒在线免费观看 | 国产精品毛片一区二区在线看 | 尤物在线观看视频 | 日本xxxx色 | 免费色视频 | 精品乱码一区二区三四区视频 | 国产国语亲子伦亲子 | 国产成人精品视频ⅴa片软件竹菊 | 少妇又色又紧又爽又刺激视频 | 国产老女人精品毛片久久 | 97精品| 男人天堂成人 | 国产91福利 | 丰满熟女人妻一区二区三区 | 人人干人人玩 | 中文字幕精品一区 | 日本乱论视频 | 国产精品自拍亚洲 | 色天天色 | 最好看的2019中文大全在线观看 | 亚洲午夜精选 | 亚洲精品7777 | 久久这里只有精品99 | aaa日韩| 国产日韩欧美成人 | 欧美日韩视频 | 亚洲69av | 好吊妞在线 | 亚洲欧洲精品成人久久奇米网 | 欧美国产在线观看 | 久久精品一区二区免费播放 | 久久福利网站 | 国产av人人夜夜澡人人爽麻豆 | 精品中文在线 | 久久综合免费 | 丰满人妻一区二区三区大胸 | 久久午夜剧场 | 色av网 | 亚洲在线色 | 亚洲婷婷在线观看 | 四虎永久在线 | 操女人逼逼视频 | a在线视频 | 性饥渴的农村熟妇 | 欧美a在线 | 澳门黄色 | 国产靠逼网站 | 今天高清视频在线观看视频 | 中文字幕在线观看播放 | 精品国自产拍在线观看 | 亚洲97色 | 东京热一区二区三区四区 | 毛利兰被扒开腿做同人漫画 | 蜜桃91丨九色丨蝌蚪91桃色 | 清清草在线视频 | 国产亚洲色婷婷久久 | 亚洲精品一区二区三区四区五区 | 日本午夜小视频 | 亚洲欧美成人 | 成人免费无码大片a毛片 | 东北熟女一区二区三区 | 国产资源站 | 91精品看片 | av网站有哪些 | 亚洲精品无 | 国产99对白在线播放 | 亚洲av无码久久精品色欲 | 欧美a级在线免费观看 | 午夜福利三级理论电影 | 日韩亚洲精品在线 | 中国老太婆性视频 | 宅男深夜视频 | 日批av| 欧美日韩一级大片 | 欧美挤奶吃奶水xxxxx | 国产美女精品人人做人人爽 | 做a爰小视频| 无码粉嫩虎白一线天在线观看 | 女人舌吻男人茎视频 | 狂野欧美性猛交免费视频 | 嫩模被强到高潮呻吟不断 | 91大神精品 | 伊人狼人综合 | va欧美| 国产ts人妖系列高潮 | 国产一区二区三区精品愉拍 | 98国产精品 | 一级免费黄色大片 | 国产美女免费无遮挡 | 91热爆视频 | 隣の若妻さん波多野结衣 | 西西久久 | 日本精品国产 | 一区二区三区精品国产 | 亚洲粉嫩| 国产www视频 |