hdoj The first place of 2^n 3215 (数学技巧&预处理)

来源:互联网 发布:钢窗 知乎 编辑:程序博客网 时间:2024/06/08 16:58

The first place of 2^n

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


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 ith integer represents the number of js satisfying the condition that 2j begins with i (0≤j≤N).
 

Sample Input
01310-1
 

Sample Output
1 0 0 0 0 0 0 0 01 1 0 0 0 0 0 0 01 1 0 1 0 0 0 1 04 2 1 1 1 1 0 1 0
//题意:
有多组数据,每组数据是一个数n,当输入为-1时就结束。
让你先计算出2^i(0<=i<=n)的值后,然后统计所有的值的第一位数出现的次数。
输出:每组输出9个数,分别是1-9出现的次数。
//思路:
因为题中要求的是n<=10000.所以要计算2^n的数会很大,所以用一个数kk记录每次平方后的第一位数即可。
具体看代码:
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3f#define ll long long#define N 10010using namespace std;int map[N][11];int init(){int i,j,k;double kk=1;map[0][1]=1;for(i=1;i<=10000;i++){for(j=1;j<=9;j++){map[i][j]=map[i-1][j];}kk*=2;if(kk>10)kk/=10;map[i][(int)kk]++;}}int main(){init();int n,i,j;while(scanf("%d",&n)&&n!=-1){for(i=1;i<9;i++)printf("%d ",map[n][i]);printf("%d\n",map[n][9]);}return 0;}

0 0
原创粉丝点击