Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/rdmd
- import std.math, std.stdio, std.algorithm;
- long[40][40] choose;
- long[40][40] choose_at_most;
- long fun(long n,long m){
- long dig=1<<30;
- long pos=31;
- long res=0;
- while (pos){
- if (choose_at_most[pos-1][m] <= n) {
- n -= choose_at_most[pos-1][m];
- res += dig;
- --m;
- }
- dig>>=1;
- pos--;
- }
- return res;
- }
- string binary(long n, bool trail=true){
- return n? binary(n/2,false) ~ (n&1?'1':'0'): trail? "0": "";
- }
- void main(){
- choose[0][0]=1;
- for (int i=0; i<40; i++)
- for (int j=0; j<40; j++)
- choose[i][j]=(i?choose[i-1][j]+(j?choose[i-1][j-1]:0):j==0),
- choose_at_most[i][j]=choose[i][j]+(j?choose_at_most[i][j-1]:0);
- writeln(choose_at_most[0][0]);
- for (int i=0; i<50; i++){
- writefln("%3d = %s",cast(int)fun(i,3),fun(i,3).binary);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement