[codeforces 1288B] Yet Another Meme Problem 公式推导

[codeforces 1288B] Yet Another Meme Problem   公式推导

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1288/problem/B

Problem Lang Verdict Time Memory
B - Yet Another Meme Problem GNU C++11 Accepted 31 ms 0 KB

思路如下:

以样例中的
191 31415926
为例
[1,191]*9
[1,191]*99
[1,191]*999
[1,191]*9999
[1,191]*99999
[1,191]*999999
[1,191]*9999999
a可取1到191   191种可能,b可取9,99,999,9999,99999,999999,9999999   7种可能
输出结果为191*7=1337

上述结论推导如下
以a=191,b=9999999为例,b的位数为n,该例为7,10^7=10000000
191*9999999+191+9999999=191*10000000+9999999
根据上式,可得通式
a*b+a+b=a*(10^n)+b
计算可得a*b+a=a*(10^n)
b+1=10^n
b=10^n-1
根据上式,在1-10^9范畴内,b只有9,99,999,9999,99999,999999,9999999, 99999999,999999999  9种可能,a只要非0,均可。
答案=a可能数量*b可能数量

#include <stdio.h>
#define LL long long
int b[20];
int main(){
	int t,A,B,c=1,i,cnt;
	for(i=1;i<=9;i++)
		c*=10,b[i]=c-1;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&A,&B);
		for(i=1;i<=9;i++)
			if(b[i]>B)break;
		if(b[i]>B)cnt=i-1;
		else if(i>9)cnt=i-1;//lose the line.
		else cnt=i;
		printf("%lld\n",(LL)cnt*A);
	}
	return 0;
}
发布了477 篇原创文章 · 获赞 509 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/103982862