Luogu T299353 about exponentiation (2 stars)

First let's look at the requirements:

Still a to the mth power, but this time with a new number n.

The current calculation is a to the mth power × or ÷a to the nth power

Or let you calculate whether the result is a composite number, the composite number is Y, and output the minimum value that can be divisible by it, not the composite number is N.

The result is 1, no output.

First, we calculate the m power of a

Calculate the mth power of a, simple, only need to use the pow function

Note that this function can only be used if the cmath library is imported: include<cmath>

pow(a,m)

Second, we ask whether this is a composite number

A composite number is a number that has multiples other than 1 and itself. Simply put, it is not a prime number. We can try to write a function:

bool notprime(long long a){
	if(s==1)return 0;
	for(long long i=2;i<a;i++)if(a%i==0)return 1;
	return 0;
}//不是质数,就是合数
long timenot(long long a){
	for(long long i=2;i<a;i++)if(a%i==0)return i;
}//不用担心没倍数不返回,我们只有碰到合数时才会调用

If it is a composite number, we also need to determine the smallest divisible multiple

Next, we start writing code

#include<iostream>
#include<cmath>
using namespace std;
bool notprime(long long a){
	if(a==1)return 0;
	for(long long i=2;i<a;i++)if(a%i==0)return 1;
	return 0;
}
long timenot(long long a){
	for(long long i=2;i<a;i++)if(a%i==0)return i;
}
int main(){
    long long a,b,n,s;
    char m;
    cin>>a>>b>>n>>m;//输入
	if(m=='*')s=pow(a,b)*pow(a,n);//现在的计算是a的m次方×a的n次方
	if(m=='/')s=pow(a,b)/pow(a,n);//现在的计算是a的m次方÷a的n次方
	if(notprime(s))cout<<s<<endl<<"Y "<<timenot(s);//如果是合数,那么输出数字、字符和倍数
	else if(s==1)cout<<1<<endl;//如果是1,则只输出1
	else cout<<s<<endl<<"N"<<endl;//如果不是合数,就输出数字和字符
}

AC, no problem! 

Guess you like

Origin blog.csdn.net/m0_72193379/article/details/128271854