HDU 3215 The first place of 2^n(数论-其他)

来源:互联网 发布:淘宝入驻条件2017 编辑:程序博客网 时间:2024/06/06 10:07

The first place of 2^n

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
 

Source
2009 Shanghai Network Contest Host by DHU
 

Recommend
zhuweicong

题目大意:

题目大意就是计算2^0到2^n这n个数首位为1的次数,2的次数,...9的次数。


解题思路:

log10,纯数学。注意精度误差。

X = pow(10, i * log10(2.0) - int(i * log10(2.0)));

自己想为什么。


参考代码:
#include <iostream>#include <cstring>#include <cmath>using namespace std;const int MAXN = 10010;const double eps = 1e-10;int first[MAXN], cnt[10], n;void init() {    for (int i = 0; i <= 10000; i++) {        first[i] = int(pow(10, i * log10(2.0) - int(i * log10(2.0))) + eps);    }}void solve() {    memset(cnt, 0, sizeof(cnt));    for (int i = 0; i <= n; i++) {        cnt[first[i]]++;    }    for (int i = 1; i <= 9; i++) {        if (i > 1) cout << " ";        cout << cnt[i];    }    cout << endl;}int main() {    ios::sync_with_stdio(false);    init();    while (cin >> n && n != -1) {        solve();    }    return 0;}


0 0
原创粉丝点击