您的位置:首页 > 其它

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

2016-03-01 16:48 405 查看

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


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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).
 

[align=left]Sample Input[/align]

0
1
3
10
-1

 

[align=left]Sample Output[/align]

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//题意:有多组数据,每组数据是一个数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 10010
using namespace std;
int map
[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
[i]);
printf("%d\n",map
[9]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: