AcWing 4272. 天长地久
原题链接
简单
作者:
YAX_AC
,
2024-12-10 14:06:53
,
所有人可见
,
阅读 3
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
LL n,m,k;
int T;
//最后一位一定是9
//n = m-(c*9)+1-9;//c是除最后一位是9,其余是9的个数
//prime(__gcd(n,m))>2;
bool prime(LL x)
{
if(x<2) return 0;
for(int i = 2; i<=x/i; i++)
if(x%i == 0) return 0;
return 1;
}
LL sum(LL x)
{
LL s = 0;
while(x)
{
s+=x%10;
x/=10;
}
return s;
}
void find(LL k,LL m,vector<PII> &ans)
{
LL start = pow(10,k-1);
LL end = start*10;
for(LL i = start+9; i<end; i+=10)
{
if(sum(i) == m)
{
LL n = sum(i+1);
LL f = __gcd(n,m);
if(f>2 && prime(f))
{
ans.push_back({n,i});
}
}
}
}
int main()
{
cin>>T;
for(int i = 1; i<=T; i++)
{
cin>>k>>m;
printf("Case %d\n",i);
vector<PII> ans;
find(k,m,ans);
if(ans.size())
{
sort(ans.begin(),ans.end());
for(int i = 0; i<ans.size(); i++)
cout<<ans[i].first<<' '<<ans[i].second<<endl;
}
else puts("No Solution");
}
return 0;
}