Educational Codeforces Round 80 (Rated for Div. 2)B. Yet Another Meme Problem

B. Yet Another Meme Problem

题目链接B. Yet Another Meme Problem
在这里插入图片描述
题目大意
求满足1≤a≤A,1≤b≤B且满足公式a⋅b+a+b=conc(a,b)的(a,b)对的数量
解题思路
设n为数字b的位数,则conc(a,b)=a×(10^n)+b.
所以可以得到等式a×b+a+b=a×(10^n)+b
化简得a×(b+1)=a×(10^n)
所以得出b的通式b=(10^n)-1.
根据上式,在1-10^9范围内,b只有9,99,999,9999,99999,999999,9999999, 99999999,999999999 这9种可能,而a只要非0即可。
因此输出结果=a满足条件的数量*b满足条件的数量。

附上代码

  • 代码1
    用一个数组存取9种可能再与B值相比较即可
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=0x3f3f3f;
int s[15];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int t,c=1;
	cin>>t;
	for(int i=0;i<9;i++){
		c*=10;
		s[i]=c-1;
	}
	while(t--){
		ll a,b;
		cin>>a>>b;
		int i;
		for(i=0;i<9;i++){
			if(b<s[i])
				break;
		}
		if(b<s[i]||i==9) b=i;
		else b=i+1;
		ll ans=a*b;
		cout<<ans<<endl;
	}
	return 0;
}
  • 代码2
    更加暴力
    除了代码比较长没什么毛病,更容易理解
    ~
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=0x3f3f3f;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		ll a,b;
        cin>>a>>b;
        if(b<9)
            cout<<"0"<<endl;
        else{
            if(b<99)
                cout<<a<<endl;
            else if(b<999)
                cout<<a*2<<endl;
            else if(b<9999)
                cout<<a*3<<endl;
            else if(b<99999)
                cout<<a*4<<endl;
            else if(b<999999)
                cout<<a*5<<endl;
            else if(b<9999999)
                cout<<a*6<<endl;
            else if(b<99999999)
                cout<<a*7<<endl;
            else if(b<999999999)
                cout<<a*8<<endl;
            else
                cout<<a*9<<endl;
        }
	}
	return 0;
}
发布了2 篇原创文章 · 获赞 1 · 访问量 113

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/103997262