ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-08-02 09:04:30  阅读:395  来源: 互联网

标签:prime cout 题解 Gregor 736 素数 p% 余数 Div


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有