Hdu 5051 Fraction(本福特定律)

题目链接

Fraction

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


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
2014 ACM/ICPC Asia Regional Shanghai Online
 

Recommend
hujie

题意:给三个数n,b,q。a[ i ]=b*q^i。i->无穷,求a数列中前缀为n的概率。


题解:当q=1,10,100,1000的情况特判。其余情况满足本福特定律。

本福特定律说明在b进位制中,以数n起头的数出现的机率为logb(n + 1) − logb(n) .本福特定律不但适用于个位数字,连多位的数也可用。

本福德定律的应用条件是:
(1)数据不能是规律排序的,比如发票编号、 身份证号码等;
(2)数据不能经过人为修饰。
至于这题为什么满足本福特定律,我也不太清楚。。。

代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<math.h>
#define nn 110
#define inff 0x3fffffff
typedef __int64 LL;
using namespace std;
int n,b,q;
vector<int>a,c;
double solve()
{
    bool ok=false;
    if(q==1)
    {
        while(b)
        {
            if(n==b)
            {
                ok=true;
                break;
            }
            b/=10;
        }
    }
    else
    {
        b*=10000;
        while(b)
        {
            if(n==b)
            {
                ok=true;
                break;
            }
            b/=10;
        }
    }
    if(ok)
        return 1.0;
    else
        return 0.0;
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=1;
    while(t--)
    {
        cin>>n>>b>>q;
        printf("Case #%d: ",cas++);
        if(q==1||q==10||q==100||q==1000)
        {
            printf("%.5lf\n",solve());
        }
        else
        {
            double ans=log10(n+1.0)-log10(n*1.0);
            printf("%.5lf\n",ans);
        }
    }
    return 0;
}


你可能感兴趣的:(数学,ACM)