其他分享
首页 > 其他分享> > Codeforces Round #736 (Div. 2) A题题解

Codeforces Round #736 (Div. 2) A题题解

作者:互联网

2021-08-02

08:34:33

链接

https://codeforces.com/contest/1549/problem/A

题目内容

 

 

                                A. Gregor and Cryptography

                        time limit per test1 second

                      memory limit per test256 megabytes

                        inputstandard input

                           outputstandard output

Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them.

Gregor's favorite prime number is P. Gregor wants to find two bases of P. Formally, Gregor is looking for two integers a and b which satisfy both of the following properties.

Pmoda=Pmodb, where xmody denotes the remainder when x is divided by y, and2≤a<b≤P.Help Gregor find two bases of his favorite prime number!

 

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000).

Each subsequent line contains the integer P (5≤P≤109), with P guaranteed to be prime.

Output

Your output should consist of t lines. Each line should consist of two integers a and b (2≤a<b≤P). If there are multiple possible solutions, print any.

Example

inputCopy

2

17

5

outputCopy

3 5

2 4

Note

The first query is P=17. a=3 and b=5 are valid bases in this case, because 17mod3=17mod5=2. There are other pairs which work as well.

In the second query, with P=5, the only solution is a=2 and b=4.

 

题目意思:

就是输入的第一行有一个输入t(1<=t<=1000),表示有t个测试数据

接下来有t个素数,每个素数都用p来表示。且5<=p<=10^9

题目要求:在每个数据里都要找出两个数a,b,使得p%a==p%b,如果不止一组解,任意输出一组解即可

(2<=a<b<=p)

 

由题目我们可知,p是素数,素数可以化为以下形式

P=a·x1+r = b·x2+r

a,b即为我们所求

左右相消,我们可以得到   a·x1 = b·x2

但是现在有四个未知量倘若一个一个试肯定会超时,那么我们缩小范围,假设x1与x2的比例关系

假设为2:1时,那么a=2时,b=4,但是这样又可以举出一个反例,令p=23,那么p%a=23%2=1,p%b=23%4=3

所以这个方法也不可取

当时我做到这里,其实是有点头大的,思维题确实是想不出就写不出,但是我通过p=23这个反例获得了灵感

能不能将p的余数固定,不像上面的反例一样有不确定性

这里我们知道,p%a时,如果你的a越大,那么余数的可能性就越大,可以是1~a-1,如果要固定余数的话,只能将a=2。

所以我将a固定为2,因为p是素数,所以p%2=1,那么这样我们的余数就固定下来了

在这里我们该怎么求b呢,还是素数变形

P=a·b+r 

这里r=1,a=2

所以  b  =(P-r)/a  =  (p-1)/2

写到这里,我们就可以输出结果了

cout<<2<<' '<<(p-1)/2<<endl;

这就是本题的核心代码了

 

注意,本题还需要特判一个情况,就是p=5的情况,当p=5时,程序输出的是 2 2

不是2 4,所以加一个特判

if(n==5){
            cout<<2<<" "<<4<<endl;
            continue;
}

上完整代码

#include<iostream>

using namespace std;

int main()

{

    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n==5){
            cout<<2<<" "<<4<<endl;
            continue;
        }
        n-=1;
        cout<<2<<" "<<n/2<<endl;
    }
    return 0;
}

  

标签:prime,cout,题解,Gregor,736,素数,p%,余数,Div
来源: https://www.cnblogs.com/DragonMao/p/15088459.html