列表

详情


NC24744. [USACO 2010 Nov B]Math Practice

描述

One lovely afternoon, Bessie's friend Heidi was helping Bessie review for her upcoming math exam.
Heidi presents two integers A (0 <= A <= 45) and B (1 <= B <= 9) to Bessie who must respond with an integer E in the range 1..62. E is the smallest integer in that range that is strictly greater than A and also has B as the first digit of 2 raised to the E-th power. If there is no answer, Bessie responds with 0.
Help Bessie correctly answer all of Heidi's questions by calculating her responses.
By way of example, consider A=1 and B=6. Bessie might generate a table like this:          E         2^E    First digit of 2^E          2          4            4          3          8            8          4         16            1          5         32            3          6         64            6      <-- matches B Thus, E=6 is the proper answer. 
NOTE: The value of 2^44 does not fit in a normal 32-bit integer.

输入描述

* Line 1: Two space-separated integers: A and B

输出描述

* Line 1: A single integer E calculated as above

示例1

输入:

1 6 

输出:

6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 504K, 提交时间: 2019-10-19 12:52:58

#include<iostream>
#include<math.h>
using namespace std;

int main(void)
{
int A,B;
while(scanf("%d%d",&A,&B)==2)
{
int i;
double d;
int digit;
int p;
for(i=A+1;i<=62;i++)
{
d=i*log10(2.0);
p=(int)(d);
digit=(int)pow(10.0,d-p);
if(digit==B)
{
printf("%d\n",i);
break;
}
}
if(i==63)
printf("0\n");
}
return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396K, 提交时间: 2021-08-07 11:04:58

#include <cstdio>
int a,b,E=1;
long long f=1,p=2;
int main() {
	scanf("%d %d",&a,&b);
    while(E<=62&&(p/f!=b||E<=a)) {
        ++E;
        p<<=1;
        while(p/f>9) f=(f<<3)+(f<<1);
    }
    if(E>62) printf("0");
    else printf("%d",E);
    return 0;
}

Python(2.7.3) 解法, 执行用时: 17ms, 内存消耗: 2924K, 提交时间: 2019-10-19 08:26:16

tmp = map(int, raw_input().split())
a = tmp[0]
b = tmp[1]
ans = 0
for i in range(a + 1, 63) :
  if int(str(1 << i)[0]) == b :
    ans = i
    break
print ans

Python3(3.5.2) 解法, 执行用时: 28ms, 内存消耗: 3548K, 提交时间: 2019-10-21 08:29:52

a, b = map(int, input().split())
for i in range(a + 1, 63):
    if str(2 ** i)[0] == str(b):
        print(i)
        exit(0)
print(0)

上一题