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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[USACO12FEB]牛的IDCow IDs

發布時間:2024/4/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [USACO12FEB]牛的IDCow IDs 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitious, and only labels cows with binary numbers that have exactly K "1" bits (1 <= K <= 10). The leading bit of each label is always a "1" bit, of course. FJ assigns labels in increasing numeric order, starting from the smallest possible valid label -- a K-bit number consisting of all "1" bits. Unfortunately, he loses track of his labeling and needs your help: please determine the Nth label he should assign (1 <= N <= 10^7).

FJ給他的奶牛用二進制進行編號,每個編號恰好包含K 個"1" (1 <= K <= 10),且必須是1開頭。FJ按升序編號,第一個編號是由K個"1"組成。

請問第N(1 <= N <= 10^7)個編號是什么。

輸入輸出格式

輸入格式:

?

  • Line 1: Two space-separated integers, N and K.

?

輸出格式:

?

輸入輸出樣例

輸入樣例#1:
7 3 輸出樣例#1: 10110? 我們先將這個串用足夠大小保存,足夠大的話我們可以添加前導0,到最后從第一個非0位輸出即可,也就是說我們要找到一個m,使得C(m,k) >= n,可以二分求m 這題最坑的是為了防止溢出longlong,所以要將k分情況制定二分右界

如果做到第i位,還要填j個1,那么這一位填0的方案數就是C(i-1,j),即還剩i-1位可以填j個1的方案數。

如果這個數小于n,那么這一位填1,并且n要減去這個數,否則這一位填0。

myys

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 ll n; 8 int k,cnt,m,ans[1001]; 9 ll C(int x,int y) 10 {int i; 11 if (x>y) return 0; 12 ll s=1; 13 for (i=y;i>y-x;i--) 14 s*=i; 15 for (i=x;i>=2;i--) 16 s/=i; 17 return s; 18 } 19 int main() 20 {int i; 21 cin>>n>>k; 22 if (k==1) 23 { 24 cout<<1; 25 for (i=n-1;i>=1;i--) 26 { 27 printf("0"); 28 } 29 return 0; 30 } 31 int l=1,r; 32 if (k>=10) 33 r=600; 34 else if (k>=7) 35 r=1000; 36 else r=8000; 37 while (l<=r) 38 { 39 int mid=(l+r)/2; 40 if (C(k,mid)>=n) m=mid,r=mid-1; 41 else l=mid+1; 42 } 43 cnt=0; 44 for (i=m;i>=1;i--) 45 { 46 ll p=C(k,i-1); 47 if (p<n) 48 { 49 k--; 50 ans[i]=1; 51 n-=p; 52 if (cnt==0) 53 { 54 cnt=i; 55 } 56 } 57 if (k==0||n==0) 58 { 59 break; 60 } 61 } 62 for (i=cnt;i>=1;i--) 63 printf("%d",ans[i]); 64 }

?

轉載于:https://www.cnblogs.com/Y-E-T-I/p/7656933.html

總結

以上是生活随笔為你收集整理的[USACO12FEB]牛的IDCow IDs的全部內容,希望文章能夠幫你解決所遇到的問題。

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