20140930 【 数论 -- 本福特定律 】 2014-上海区域赛-网络预选赛 hdoj 5051 Fraction

17 篇文章 0 订阅
9 篇文章 0 订阅

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
Author ID 
Password 
  Register new ID
BestCoder官方群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Fraction

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 251    Accepted Submission(s): 61


Problem Description
Given a number n, and a geometric progression a i = b * q i, i ≥ 0, what is the fraction of the elements of that progression with decimal notation that has the decimal notation of n as prefix ?

More formally, if c i out of the first i elements of the progression start with n in decimal notation, you need to find the limit . It is guaranteed that the limit always exists.

For example, n = 7, b = 1, q = 2. About 5.799% of all powers of two start with 7. (the smallest one is 2 46 = 70368744177664)
 

Input
The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.

Each case contains three integers n,b and q. (1 ≤ n, b, q ≤ 1000)
 

Output
For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then output one floating number – the sought fraction. Round your answer to the 5th decimal place.
 

Sample Input
      
      
2 7 1 2 1 1 1
 

Sample Output
      
      
Case #1: 0.05799 Case #2: 1.00000
 

Source
 

Recommend
hujie
 

Statistic |  Submit |  Discuss |  Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2014 HDU ACM Team. All Rights Reserved.
Designer & Developer Wang Rongtao LinLe GaoJie GanLu
Total 0.001751(s) query 1, Server time : 2014-09-30 14:16:08, Gzip disabled








水的一地的题目。

可是我只能说:“我根本就不知道有这定律!!!”


百度百科:本福特定律


主要思想:

在 b 进制中,以数字 n 开头的数出现的几率是:logb(n+1) - logb(n)


除去某些特判的情况,套公式就行了。








#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;

int n, b, k;
const char answer[][10] = {"0.00000", "1.00000"};

int check(int bb){
    while( bb ){
        if( n==bb )  return 1;
        bb /= 10;
    }
    return 0;
}

int main(){
    int T;
    scanf("%d", &T);
    for(int tt=1; tt<=T; tt++){
        scanf("%d%d%d", &n, &b, &k);
        printf("Case #%d: ", tt);
        int flag = 0;
        if( k==1 )
            puts( answer[ check(b) ] );
        else if( 10==k || 100==k || 1000==k )
            puts( answer[ check(b*1000000) ] );
        else
            printf("%.5lf\n", ( log(n+1)-log(n) )/log(10) );
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值