POJ 2635 The Embarrassed Cryptographer //大数求余 素数打表 同余法

The Embarrassed Cryptographer

Description

The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of users, which is now in use in his company. The cryptographic keys are created from the product of two primes, and are believed to be secure because there is no known method for factoring such a product effectively.
What Odd Even did not think of, was that both factors in a key should be large, not just their product. It is now possible that some of the users of the system have weak keys. In a desperate attempt not to be fired, Odd Even secretly goes through all the users keys, to check if they are strong enough. He uses his very poweful Atari, and is especially careful when checking his boss' key.

Input

The input consists of no more than 20 test cases. Each test case is a line with the integers 4 <= K <= 10 100 and 2 <= L <= 10 6. K is the key itself, a product of two primes. L is the wanted minimum size of the factors in the key. The input set is terminated by a case where K = 0 and L = 0.

Output

For each number K, if one of its factors are strictly less than the required L, your program should output "BAD p", where p is the smallest factor in K. Otherwise, it should output "GOOD". Cases should be separated by a line-break.

Sample Input

143 10
143 20
667 20
667 30
2573 30
2573 40
0 0

Sample Output

GOOD
BAD 11
GOOD
BAD 23
GOOD
BAD 31

Source

[Submit]   [Go Back]   [Status]   [Discuss]

We have carefully selected several similar problems for you:  2300 2308 2301 2305 2306




参考网址  ::::https://blog.csdn.net/sr_19930829/article/details/37991865

题意:给定一个大数,它可以由两素数相乘得到,如果两个素数中最小值小于l,就输出BAD,并且输出这个素数因子;如果大于l就输出GOOD

思路:先把素数筛选出来,然后把输入的大数从后往前,每三位存放在一起,利用同余法来求余,如果能被素数除尽并且素数因子小于l就输出BAD 和素数因子,如果最小的素数因子大于l就输出GOOD;

同余与模算术:

(a+b)%n=((a%n)+(b%n))%n;

(a*b)%n=((a%n)*(b%n))%n;

大数取模:例如数据1234 把大整数写成“自左向右”的形式  1234=((1*10+2)*10+3)*10+4,利用公式,逐步取模


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
int main()
{
  char n[1000];
  int m;
  scanf("%s %d",n,&m);	
  int len=strlen(n);
  int ans=0;
  for(int i=0;i<len;i++)
    ans=(int) (((long long )ans*10+n[i]-'0')%m);  	
   printf("%d\n",ans);
   return 0;
} 

素数打表

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=1000005;
int prime[maxn+1]; //存放素数 
int isprime[maxn+1];//作为素数标记数组,注意开的数组的大小 
int primelen=0;//素数数组的长度 
int main()
{
   for(int i=0;i<=maxn; i++) //筛选素数
	{
		isprime[i]=1;  
	}
     isprime[0]=isprime[1]=0;
	for(int i=2;i<=maxn;i++)
	{
		if(isprime[i])
		{
			prime[primelen++]=i;
			//cout<<i<<endl;
			for(int j=2*i;j<=maxn;j=j+i)
			{
				isprime[j]=0;
			}
		}
	}
	for(int i=0;i<21;i++)  //只输出了前21个素数 
	{
		cout<<prime[i]<<" ";
	}
	cout<<endl;
	for(int i=0;i<21;i++)
	{
	   cout<<isprime[i]<<" ";
	}
	cout<<endl;
} 

题目代码 :::


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=1000005;
int prime[maxn+1]; //存放素数 
int isprime[maxn+1];//作为素数标记数组,注意开的数组的大小 
int primelen=0;//素数数组的长度  
int num[maxn];//存放大数 数组中的数据是倒着存的(每三位存放一个) 
//例如 12345 存放在 数组中的数据就是 12  345 (倒着存的 
char s[maxn];//输入的大数 
int l;//输入的l 是任意的  
int k;
int len; //大数的长度 
 
void sushu(int n)  //筛选素数  
{
	for(int i=0;i<=n; i++)
	{
		isprime[i]=1;  
	}
     isprime[0]=isprime[1]=0;
	for(int i=2;i<=n;i++)
	{
		if(isprime[i])
		{
			prime[primelen++]=i;
			//cout<<i<<endl;
			for(int j=2*i;j<=n;j=j+i)
			{
				isprime[j]=0;
			}
		}
	}
}

void change()  //用来往num数组中存放大数 
{
	k=0;
	int a=len/3; //判断输入的大数的长度有个长度为3的 
	int b=len%3;//能否被3整除,不能整除的话剩下长度为多少的数据
	//例如 12345 len=5 5%3=2   剩下长度为2(即 12   倒着来 
	int temp=0;
	if(b) //能被3整除 b=0 就直接三位三位存放在num数组中,如果不能被整除,就要先把剩余的存放在num数组中 
	{
		for(int i=0;i<b;i++)
			temp=temp*10+s[i]-'0';
			num[k++]=temp;//存放大数  
	}
	int cnt=0;
	//int start=b+3*cnt;
	for(int i=1;i<=a;i++)
	{
		 temp=0;
		 int start=b+3*cnt;//用来控制存放时的开始位置  从b位置开始  依次增加3  
		for(int j=0;j<3;j++)  
		{
		  temp=temp*10+s[start+j]-'0';	
		}
		num[k++]=temp;//存放大数 
		cnt++;
	}
}
bool mod(int n) //大数取余法   把大的数据逐步取模这样就会避免最后取模时因数据太大而超时
{
  int m=0;   //m的初值要设置为0  
  for(int i=0;i<k;i++)
  {
  	 m=(m*1000+num[i])%n;
  }	
  if(m==0)
  {
  	return true;
  }
  else
  {
  	return false;
  }
}
int main()
{
	sushu(maxn); 
	
	while(cin>>s>>l && s[0]!='\0' && l)
	{
		len=strlen(s);
		change();
		bool ok=1;
		int ans=0;
		for(int i=0;i<primelen;i++)
		{
			if(mod(prime[i]) && prime[i]<l) //如果能被整除 并且整除的素数因子小于l  
			{
			   ok=0;
			   ans=prime[i];
			   break;
			}
			if(prime[i]>=l) //如果素数因子 大于l 就退出 证明不会被整除 
			   break;
		}
		if(ok)
		{
			cout<<"GOOD"<<endl;
	    }
	    else
	    {
	    	cout<<"BAD "<<ans<<endl;
		}
	} 
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方,为未来的学术或职业生涯打下坚实的基础。
### 内容概要 这份《计算机试卷1》包含多个部分,主要覆盖了计算机基础知识、操作系统应用、文字处理、电子表格、演示文稿制作、互联网应用以及计算机多媒体技术。试卷以单选题开始,涉及计算机历史、基本概念、硬件组成、软件系统、网络协议等。接着是操作应用部分,要求考生在给定的软件环境中完成一系列具体的计算机操作任务。 ### 适用人群 本试卷适用于计算机科学与技术、信息技术相关专业的学生,以及准备计算机水平考试或职业资格认证的人士。它适合那些希望检验和提升自己计算机操作能力的学习者,也适用于教育工作者作为教学评估工具。 ### 使用场景及目标 1. **学习评估**:作为教育机构的课程评估工具,帮助教师了解学生对计算机基础知识的掌握程度。 2. **自学检验**:供个人自学者检验自己的计算机操作技能和理论知识,为进一步学习提供方向。 3. **职业发展**:为职场人士提供计算机技能的自我提升途径,增强其在信息时代的竞争力。 4. **考试准备**:为准备计算机相关考试的考生提供实战演练的机会,加强考试自信。 5. **教学资源**:教师可以将其作为教学资源,设计课程和实验,提高教学效果。 试卷的目标是通过理论知识的测试和实践技能的操作,全面提升考生的计算机应用能力。考生应掌握从基础的计算机组成原理到复杂的数据处理、演示文稿制作、网络应用以及多媒体技术处理等多方面技能。通过本试卷的学习与练习,考生将能够更加熟练地使用计算机解决实际问题,为未来的学术或职业生涯打下坚实的基础。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值