2^n的第一位数字 soj 3848 mathprac
Time Limit: 3000 MS Memory Limit: 65536 K
mathprac
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 6
mathprac
Description
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.
Input
* Line 1: Two space-separated integers: A and B
Output
* Line 1: A single integer E calculated as above
Sample Input
1 6
Sample Output
6
题意:
就是求2^n的第一位数字;
由于2^n=t*10^k,那么也就是求t的第一位。
log10(2^n)=n*log10(2)
=log10(t*10^k)
=log10(t)+k k为整数
求出n*log10(2)减去k即可求得log10(t),然后求出10^log10(t),对其取整,即为所求。
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?