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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Knapsack Cryptosystem【折半+查找】

發布時間:2025/3/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Knapsack Cryptosystem【折半+查找】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/889/D
來源:牛客網

Amy asks Mr. B problem D. Please help Mr. B to solve the following problem.

Amy wants to crack Merkle–Hellman knapsack cryptosystem. Please help it.

Given an array {ai} with length n, and the sum s.
Please find a subset of {ai}, such that the sum of the subset is s.

For more details about Merkle–Hellman knapsack cryptosystem Please read
https://en.wikipedia.org/wiki/Merkle%E2%80%93Hellman_knapsack_cryptosystem
https://blog.nowcoder.net/n/66ec16042de7421ea87619a72683f807
Because of some reason, you might not be able to open Wikipedia.
Whether you read it or not, this problem is solvable.
輸入描述:
The first line contains two integers, which are n(1 <= n <= 36) and s(0 <= s < 9 * 1018)
The second line contains n integers, which are {ai}(0 < ai < 2 * 1017).

{ai} is generated like in the Merkle–Hellman knapsack cryptosystem, so there exists a solution and the solution is unique.
Also, according to the algorithm, for any subset sum s, if there exists a solution, then the solution is unique.
輸出描述:
Output a 01 sequence.

If the i-th digit is 1, then ai is in the subset.
If the i-th digit is 0, then ai is not in the subset.
示例1
輸入
復制
8 1129
295 592 301 14 28 353 120 236
輸出
復制
01100001
說明
This is the example in Wikipedia.
示例2
輸入
復制
36 68719476735
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483648 4294967296 8589934592 17179869184 34359738368
輸出
復制
111111111111111111111111111111111111

題目大意:
先輸入一個數字nnn,代表有一個大小為nnn的數組(1≤n≤36)(1\le n \le 36)(1n36),再輸入一個數字sss,下面一行輸入nnn個數字,要從這nnn個數字里面選取任意個數字使其和等于sss,通過輸出一串長度為nnn的01字符串來表示選或不選。

解題思路:
首先想到的是枚舉所有選擇的情況,但發現其時間復雜度已經到達了2362^{36}236,明顯會超時,因此想到折半的方法,將其時間復雜度降為2182^{18}218,接近1e61e61e6,也就是將數組分為兩部分,枚舉前半部份所有的選擇情況,將其和存在一個數組中,同理處理后半部分,后面判斷其和是否能組成sss,僅需枚舉前半部分的所有情況,通過用 s?s-s?前半部分的和 ,查找其在后半部分是否存在即可。

代碼:

//#pragma GCC optimize(3,"Ofast","inline") #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; #define inf 0x3f3f3f3f #define rep(i,l,r) for(int i=l;i<=r;i++) #define lep(i,l,r) for(int i=l;i>=r;i--) #define ms(arr) memset(arr,0,sizeof(arr)) //priority_queue<int,vector<int> ,greater<int> >q; const int maxn = (int)1e5 + 5; const ll mod = 1e9+7; ll arr[40]; struct node1 {ll num;int id; }sum_qian[1001000]; struct node2 {ll num;int id; }sum_hou[1001000]; bool cmp1(node1 a,node1 b) {return a.num<b.num; } bool cmp2(node2 a,node2 b) {return a.num<b.num; } ll get_sum(int id,int len) {ll sum=0;while(id) {if(id&1) {sum+=arr[len];}id>>=1;len++;}return sum; } int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);//ios::sync_with_stdio(0),cin.tie(0);int n;ll s;cin>>n>>s;rep(i,1,n) {cin>>arr[i];}int nape=n/2;for(int i=0;i<(1<<nape);i++) {sum_qian[i].num=get_sum(i,1);sum_qian[i].id=i;}int len_qian=(1<<nape);int t1=nape;int start=nape+1;nape=n-nape;for(int i=0;i<(1<<nape);i++) {sum_hou[i].num=get_sum(i,start);sum_hou[i].id=i;}int len_hou=(1<<nape);int t2=nape;sort(sum_qian,sum_qian+len_qian,cmp1);sort(sum_hou,sum_hou+len_hou,cmp2);int qian=-1,hou=-1;for(int i=0;i<len_qian;i++) {ll nape;if(s>=sum_qian[i].num)nape=s-sum_qian[i].num;else continue;int l=0,r=len_hou;int id=-1;while(r-l>1) {int mid=(r+l)>>1;if(sum_hou[mid].num==nape) {id=sum_hou[mid].id;break;}else if(sum_hou[mid].num>nape) {r=mid;}else {l=mid;}}if(id!=-1) {qian=sum_qian[i].id;hou=id;break;}}if(qian!=-1&&hou!=-1) {for(int i=1;i<=t1;i++) {cout<<(qian&1);qian>>=1;}for(int i=1;i<=t2;i++) {cout<<(hou&1);hou>>=1;}cout<<endl;}return 0; }

總結

以上是生活随笔為你收集整理的Knapsack Cryptosystem【折半+查找】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 中文字幕麻豆 | youjizz中国少妇| 麻豆亚洲精品 | 国产女主播在线观看 | 91精品综合久久久久久五月天 | 五月婷婷婷婷 | 亚洲综合在线中文字幕 | 日本一区二区在线不卡 | 久久久国产精品一区二区三区 | 玖玖爱av | 日本一级理论片在线大全 | av黄色在线播放 | 午夜激情免费 | 国产精选视频在线观看 | 美丽的姑娘在线观看免费 | 91av免费观看 | 最近更新中文字幕 | 国产一区二区三区黄片 | 亚洲男女av | 欧美日韩电影一区 | 色老头在线一区二区三区 | 免费在线黄色片 | 黄色正能量网站 | 亚洲色图20p | 成人网av| 先锋影音av在线 | 一级黄色大片免费观看 | 一区二区www | 国产青草视频在线观看 | 国产精品欧美日韩 | 毛片无码一区二区三区a片视频 | 人妻丝袜一区二区三区 | 午夜在线网站 | 五月天开心网 | 欧美精品黄 | 九九热色 | 免费不卡毛片 | 伊人网欧美| 欧美日韩国产区 | 综合亚洲欧美 | 亚洲激情在线观看视频 | 男人天堂2014 | 好爽…又高潮了毛片免费看 | 日韩精品在线观看一区 | 最新精品国产 | 日韩aa | 国产亚洲午夜 | 四虎中文字幕 | 国产最新精品视频 | 欧美xxxx免费虐 | 涩涩免费网站 | 少妇高潮露脸国语对白 | 久久三级| 国产精品毛片va一区二区三区 | 午夜一级影院 | 欧美日韩中文在线视频 | 亚洲综合大片69999 | 91人人干| 天天爽夜夜爽 | 中文字幕中文在线 | 天堂a在线 | 五月婷婷激情小说 | 久久久久久久久久免费视频 | 精品99在线观看 | av在线播放免费 | 亚洲色精品三区二区一区 | 69色视频| 亚洲一区二区三区免费视频 | 在线黄色av网站 | 91丨国产| 国产全肉乱妇杂乱视频 | 国产稀缺真实呦乱在线 | 18pao国产成视频永久免费 | 国产亚洲精品久久久 | 国产精品久久久久久久免费大片 | 亚洲av无码专区在线电影 | 久久爰 | 欧美精品极品 | 精品一区二区三区四区视频 | 免费毛片一级 | 青娱乐最新地址 | 亚洲国产日韩一区二区 | 午夜黄色在线 | 台湾150部性三级 | 中文字幕日韩三级 | 精国产人伦一区二区三区 | 丁香花电影高清在线阅读免费 | 波多野结衣视频免费看 | 欧美日韩视频一区二区三区 | 大香焦久久 | 一本一道久久a久久 | 正在播放老肥熟妇露脸 | 成人福利在线观看 | 午夜精品久久久久久久99老熟妇 | 黄色wwwww | 在线视频在线观看 | 国产不卡视频一区二区三区 | 麻豆传媒一区 | 免费国产一区 |