H - ProblematicPublicKeys

ProblematicPublicKeys

On February 15, 2012, the New York Times reported a flaw in the method of generating keys for a public-key encryption system (“Researchers Find a Flaw in a Widely Used Online Encryption Method" by John Markoff). This flaw enables an attacker to determine private keys given a set of flawed public keys.
Your job is to write a program that takes flawed public keys and determines the corresponding private keys. For the purposes of this problem, a private key consists of a pair of prime numbers,

2 < K1,K2 < 2^31

and the corresponding public key consists of the product K1 * K2.

Input
The first line of the input contains an integer value, M (2 <= M <= 100). M is the number of input lines that follow. Each of the M lines contains a single “public key." Each public key is the product of exactly two prime numbers and will fit in a 32-bit unsigned integer.

Output
The output lines produced by your program should contain the unique prime factors of the input data values in ascending numeric order, five per line, except for the last line. Output values on the same line are separated by a single space.

Sample Input
6
221
391
713
1457
901
299

Sample Output
13 17 23 31 47
53

Sample Input 2
2
2143650557
2140117121

Sample Output 2
32717 65413 65521

题意:输入M个数,每个数a[i]是由两个质数相乘得来的。求出这些的质数,从小到大输出(不重复)。
思路:查找每个数字的质因数,将其存入set中(去重+排序),然后输出结果。

注:本题的输出,最后一个数字后面不能带有” “(空格),否则会Presentation Error。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll prime[100100];
bool check[100100];
set<ll> st;
void f()
{
    memset(check,true,sizeof(check));
    check[1]=false;
    ll n,count;
    n=100000;
        count = 0;
        for (int i = 2; i <n; i++)
        {
            if (check[i])
                prime[count++] = i;
            for (int j = 0; j < count&&i*prime[j]<100000; j++)
            {
                check[i*prime[j]] = false;
                if ( (i%prime[j]) == 0 )
                    break;
            }
    }//cout<< prime[99999] <<endl;
}
int main()
{
	f();
//	cout<< prime[199999] <<endl;
	ll n;
	cin>>n;
	for(ll i=0;i<n;i++)
	{
		ll a;
		cin>>a;
		ll j=0;
        while(1)
        {
            if(a%prime[j]==0)
            {
                st.insert(prime[j]);
                st.insert(a/prime[j]);
                break;
            }
            j++;
        }
	}
	ll i=0;
	set<ll>::iterator it;
	for(it=st.begin();it !=st.end();++it)
	{
	    it++;
	    if(it==st.end())
            break;
		it--;
		i++;
		if(i%5==0)
			cout<<*it<<'\n';
        else
        {
                cout<<*it<<" ";
        }
	}

    it--;
    cout<<*it<<endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值