HDU/HDOJ 3215 The first place of 2^n

The first place of 2^n

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 350Accepted Submission(s): 141


Problem Description
LMY and YY are mathematics and number theory lovers. They like to find and solve interesting mathematic problems together. One day LMY calculates 2n one by one, n=0, 1, 2,… and writes the results on a sheet of paper: 1,2,4,8,16,32,64,128,256,512,1024,……

LMY discovers that for every consecutive 3 or 4 results, there must be one among them whose first digit is 1, and comes to the conclusion that the first digit of 2n isn’t evenly distributed between 1 and 9, and the number of 1s exceeds those of others. YY now intends to use statistics to prove LMY’s discovery.

Input
Input consists of one or more lines, each line describing one test case: an integer N, where 0≤N≤10000.

End of input is indicated by a line consisting of -1.

Output
For each test case, output a single line. Each line contains nine integers. The i th integer represents the number of js satisfying the condition that 2 j begins with i (0≤j≤N).

Sample Input
 
 
0 1 3 10 -1

Sample Output
 
 
1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 4 2 1 1 1 1 0 1 0

Source
题目叫我们统计首位出现的次数
所以既然是第一位,而且是2^n所以很容易就想到了取对数
理论就是
2^n=a*10^L
两边同时取对数
n*lg2=lga+L
L=[n*lg2]
=====>
n*lg2-[n*lg2]=a
令t=n*lg2-[n*lg2]
a=pow(10,t);
这样在把a取整就是当前2^n的第一位数了
我的代码:(1Y。。^_^)
#include<stdio.h> #include<math.h> #include<string.h> #define eps 1e-12 int ans[10005][10]; void init() { double t,x,a; int i,k; ans[0][1]=1; for(i=1;i<=10000;i++) { x=(double)i; t=x*log10(2.0)-floor(x*log10(2.0)); a=pow(10.0,t); k=(int)(a+eps); if(k==1) { ans[i][1]=ans[i-1][1]+1; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==2) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]+1; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==3) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]+1; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==4) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]+1; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==5) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]+1; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==6) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]+1; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==7) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]+1; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]; } if(k==8) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]+1; ans[i][9]=ans[i-1][9]; } if(k==9) { ans[i][1]=ans[i-1][1]; ans[i][2]=ans[i-1][2]; ans[i][3]=ans[i-1][3]; ans[i][4]=ans[i-1][4]; ans[i][5]=ans[i-1][5]; ans[i][6]=ans[i-1][6]; ans[i][7]=ans[i-1][7]; ans[i][8]=ans[i-1][8]; ans[i][9]=ans[i-1][9]+1; } } } int main() { init(); int n,i; while(scanf("%d",&n)!=EOF) { if(n==-1) break; for(i=1;i<9;i++) printf("%d ",ans[n][i]); printf("%d\n",ans[n][9]); } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值