天长地久
https://www.acwing.com/problem/content/description/4275/
优质题解:https://www.acwing.com/solution/content/89003/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
int m,k,cnt = 0,f,c,n;
int a[10];
void dfs(int x,int sum,vector<PII>& v)
{
//if((k - x ) * 9 + sum < m) return ; //后面的位不足以组合
if(sum > m || x > k) return ;
if(x == k && sum == m && a[cnt - 1] != 0 && a[0] != 9)//第一位不能是0最后一位不能是9
{
int res = 0;
for(int i = cnt - 1; i >= 0 ; i --) res = res * 10 + a[i];
for(int i = 1; i <= c; i ++) res = res * 10 + 9;
v.push_back({n,res}),f = 1;
return ;
}
for(int i = 0; i <= 9; i ++)
{
a[cnt ++] = i;
dfs(x + 1,sum + i,v);
a[cnt--]=0;
}
}
int main()
{
int j;
cin >> j;
for(int i = 1; i <= j; i ++)
{
f = 0;
cin >> k >> m ;
cout << "Case " << i << '\n';
int x = m; vector<int>p;
for(int i = 2; i <= x / i; i ++) //分解质因数
{
if(x % i == 0)
{
p.push_back(i);
while(x % i == 0) x /= i;
}
}
if(x > 1) p.push_back(x);
if(p.back() <= 2) f = 0; //不存在大于2的质因子
else{
vector<PII>v;
for(auto it : p)
{
if(it <= 2) continue;
n = it;
for(int i = 1; n < m; i ++)
{
n = it * i;
if((m - n + 1) % 9 == 0)
{
c = (m - n + 1) / 9; //末尾有c位9
dfs(c,c * 9,v);
}
}
}
sort(v.begin(),v.end());
for(int i = 0; i < v.size(); i ++)
{
if(i != 0) puts("");
cout << v[i].first << ' ' << v[i].second;
}
}
if(f == 0) cout << "No Solution";
if(i != j) puts("");
}
return 0;
}