跳到正文

题解

10/28题解

Ten_shu_can约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年10月30日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

第一题:

要求

计算a的b次方,如果大于10的9次方就输出**-1**

解法

输入a,b,然后for循环中途循环判断最后输出结果

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long 
int a,b,n,m;
signed main(){
    cin>>n>>m;
    a=n;
    b=m;
    if(a==1){
        cout<<1;
        return 0;
    }
    if(b>33 or b==0){
        cout<<-1;
        return 0;
    }
    for(int i=1;i<m;i++){
        if(a>1000000000){
            cout<<-1;
            return 0;
        }a*=n;
    }cout<<a;


    return 0;
}

第二题(解密):

要求

解出两个质数p和q

解法

通过数学推导得到p和q的关系式

利用二分查找在合理范围内搜索p的值

验证找到的p和q是否满足条件

输出解或”NO”

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int k,n,d,e,m;
signed main(){
    cin>>k; 
    while(k--){
        cin>>n>>d>>e;
        m=n-e*d+2;
        int l=1,r=m/2,sum=-1;  
        while(l<=r){
            int p=(l+r)/2;
            int q=m-p;  
            if(p*q==n){
                sum=p;
                break;
            }else if(p*q<n) l=p+1;
            else r=p-1;
        }
        if(sum==-1) cout<<"NO"<<endl;
        else cout<<sum<<" "<<m-sum<<endl;
    }
}
原文