[2019 autumn PAT B level real subject] 7-4 days long and long (20 points)

Posted by artist19 on Sat, 05 Oct 2019 16:39:01 +0200

7-4 everlasting (20 points)

"Everlasting number" refers to a K positive integer A, which satisfies the condition that the sum of all figures of A is m, and the sum of all figures of A+1 is n, and the greatest common divisor of M and N is a prime number greater than 2. Please find out the everlasting number of topics.

Input format:

The input gives a positive integer N (= 5) in the first line, followed by a pair of K (3 < K < 10) and m (1 < m < 90) in each line, the meaning of which is as described above.

Output format:

For each pair of input K and m, Case X is first output in one line, where X is the number of the output (starting from 1); then the corresponding N and A are output in one line, separated by spaces. If the solution is not unique, each group occupies one line and outputs in incremental order of n; if it is not unique, it outputs in incremental order of A. If the solution does not exist, output No Solution in one line.

Input sample:

2

6 45

7 80

Output sample:

Case 1

10 189999

10 279999

10 369999

10 459999

10 549999

10 639999

10 729999

10 819999

10 909999

Case 2

No Solution

10 minutes 15 milliseconds Edition

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct daan{
	string A;
	int n;
	
};
bool cmp(daan d1,daan d2){
	//return (d1.A!=d2.A) ?d1.A<d2.A:d1.n<d2.n;
	return d1.A<d2.A;
}
daan da[1000000];
int gcd(int a,int b){
	if(b>a){
		int t=a;
		a=b;
		b=t;
	}
	if(b!=0){
		gcd(b,a%b);	
	}else{
		return a;
	}
}
int issu(int a){
	int b;
	for(b=2;b<=sqrt(a);b++){
		if(a%b==0){
			return 0;
		}
	}return 1;
}
int main(){
	int n;
	cin>>n;
	
	for(int i=1;i<=n;i++){
		cout<<"Case "<<i<<endl;
		int k,m;
		cin>>k>>m;
		if(m/k>9||k>=10||k<=3){
			cout<<"No Solution"<<endl;
			continue;
		}
		int n1=1;
		int flag=0;
		while(n1<=m){
			int p=gcd(m,n1);
			if(p>2&&issu(p)&&(m+1-n1)%9==0){
				int jiu=(m+1-n1)/9;
				int sheng=m-jiu*9;
				string s;
				for(int e=0;e<jiu;e++){
					s+='9';
				}
				//cout<<s<<endl;
				long long int p=1;
				for(int l=0;l<k-jiu;l++){
					p*=10;
				}
				for( long long int l=p/10;l<p-1;l++){
					int he=0;
					int ll=l;
					while(ll>0){
						he+=ll%10;
						ll=ll/10;
					}ll=l;
					if(he==sheng){
						string s3=s;
						//cout<<n1<<" "<<l<<s<<endl;
						while(ll>0){
							char c=ll%10+'0';
							s3=c+s3;
							ll=ll/10;
							//cout<<ll<<" "<<c<<endl;
						}
						//cout<<s3<<endl;
						da[flag].A=s3;
						da[flag].n=n1;
						flag++;
						
					}
				}
			}
			n1++;
		}
		if(flag==0){
			cout<<"No Solution"<<endl;
		}else{
			sort(da+0,da+flag,cmp);
			for(int o=0;o<flag;o++){
				cout<<da[o].n<<" "<<da[o].A<<endl;
			}
		}
		
	}
	return 0;
}

Overtime 12 points

 

#include<iostream>
#include<cmath>
using namespace std;
int gcd(int a,int b){
	if(b>a){
		int t=a;
		a=b;
		b=t;
	}
	if(b!=0){
		gcd(b,a%b);	
	}else{
		return a;
	}
}
int issu(int a){
	int b=2;
	for(b;b<=sqrt(a);b++){
		if(a%b==0){
			return 0;
		}
	}return 1;
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cout<<"Case "<<i<<endl;
		int k,m;
		cin>>k>>m;
		if(m/k>9||k>=10||k<=3){
			cout<<"No Solution"<<endl;
			continue;
		}
		long long int p=1;
		for(long long int l=0;l<k;l++){
			p*=10;
		}
		int n1=0,n2=0;
		int flag=0;
		long long int s1,s2;
		for(long long int l=p/10;l<p;l++){
			n1=0;
			n2=0;
			s1=l;
			s2=l+1;
			while(s1>0){
				n1+=s1%10;
				s1/=10;
			}
			//cout<<n1<<endl;
			if(n1!=m){
				continue;
			}
			while(s2>0){
				n2+=s2%10;
				s2/=10;
			}
			int pp=gcd(n1,n2);
			if(pp>2&&issu(pp)){
				flag=1;
				cout<<n2<<" "<<l<<endl;
			}
		}
		if(flag==0){
			cout<<"No Solution"<<endl;
		}
	}
	return 0;
}