HDU 6033 Add More Zero (數學)

Mr_Treeeee發表於2020-04-06

Add More Zero

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 302    Accepted Submission(s): 223


Problem Description
There is a youngster known for amateur propositions concerning several mathematical hard problems.

Nowadays, he is preparing a thought-provoking problem on a specific type of supercomputer which has ability to support calculations of integers between 0 and (2m1) (inclusive).

As a young man born with ten fingers, he loves the powers of 10 so much, which results in his eccentricity that he always ranges integers he would like to use from 1to 10k (inclusive).

For the sake of processing, all integers he would use possibly in this interesting problem ought to be as computable as this supercomputer could.

Given the positive integer m, your task is to determine maximum possible integer k that is suitable for the specific supercomputer.
 

Input
The input contains multiple test cases. Each test case in one line contains only one positive integer m, satisfying 1m105.
 

Output
For each test case, output "Case #xy" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 

Sample Input
1 64
 

Sample Output
Case #1: 0 Case #2: 19
 

Source
 

題意:
求出最大k,使10^k>=2^m-1,給出m。

point:
你敢相信這種簽到題我們這種殘疾隊做了1個半小時?????根本沒往數學方面想,光想著打表暴力求解了。
一個簡單的答案 k=m*(log2/log10).向下取整。


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
#define rt x<<1|1
#define lt x<<1
#define  LL long long

int main()
{
    int m;
    int p=0;
    while(~scanf("%d",&m))
    {
        printf("Case #%d: ",++p);
        printf("%.0f\n",floor(m*1.0*(log(2.0)/log(10.0))));
    }
}



現場AC的程式碼,因為精度問題WA了5發也是辛苦我了。//賽中知道正規的解題方案很想一頭撞死了,高中數學老師都要提刀砍我了。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
#define rt x<<1|1
#define lt x<<1
#define  LL long long
int ans[100000+5];
int main()
{
    LL now;
    for(int i=1;i<=59;i++)
    {
        now=(LL)pow(2,i);
        int l=0;
        while(now)
        {
            now/=10;
            l++;
        }
        ans[i]=l-1;
    }
    now=(LL)pow(2,59);
    for(int i=60;i<=100000;i++)
    {
        now=now*2;
        if(now>=1e18)
        {
            ans[i]=ans[i-1]+1;
            now=now/10;
        }
        else
        {
            ans[i]=ans[i-1];
        }
            
    }
    int n;
    int p=0;
    while(~scanf("%d",&n))
    {
        printf("Case #%d: %d\n",++p,ans[n]);
    }
    return 0;
}


相關文章