1118. Nontrivial Numbers
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Specialists of SKB Kontur have developed a unique cryptographic algorithm for needs of information protection while transmitting data over the Internet. The main advantage of the algorithm is that you needn't use big numbers as keys; you may easily do with natural numbers not exceeding a million. However, in order to strengthen endurance of the cryptographic system it is recommended to use special numbers - those that psychologically seem least "natural". We introduce a notion oftriviality in order to define and emphasize those numbers.
Triviality of a natural number N is the ratio of the sum of all its proper divisors to the number itself. Thus, for example, triviality of the natural number 10 is equal to 0.8 = (1 + 2 + 5) / 10 and triviality of the number 20 is equal to 1.1 = (1 + 2 + 4 + 5 + 10) / 20. Recall that a proper divisor of a natural number is the divisor that is strictly less than the number.
Thus, it is recommended to use as nontrivial numbers as possible in the cryptographic protection system of SKB Kontur. You are to write a program that will find the less trivial number in a given range.
Input
The only line contains two integers I and J, 1 ≤ I ≤ J ≤ 106, separated with a space.
Output
Output the only integer N satisfying the following conditions:
- I ≤ N ≤ J;
- N is the least trivial number among the ones that obey the first condition.
Sample
input | output |
---|---|
24 28 |
25 |
题意:
“SKB-Kontur”的专家们开发了一种独特的密码算法以满足在互联网上传送数据时的信息保密需要。这种算法的最大好处是,您不需要使用大数字作为密码——您可以方便地用小于一百万的自然数作为密码。但是,为了加强密码系统的安全性,推荐您使用特殊数字——那些心理上认为最不“自然”的数字。我们引入“Triviality”这个概念定义和强调那些数字。
一个自然数N的“Triviality”被定义它所有的Proper约数之和与它本身的比值。例如,自然数10的“Triviality”是0.8=(1+2+5)/10,自然数20的“Triviality”是1.1=(1+2+4+5+10)/20。注意一个自然数的Proper约数是指这个数的所有约数中严格地小于这个数本身的那些约数。
正因为如此,在“SKB-Kontur”密码安全系统中推荐尽可能使用Non-Trivial数字。你的任务是写一个程序,在给定的范围内找出“Triviality”值最小的数字。
思路:其实就是暴力,只不过加了一些优化;
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<string> 7 8 9 using namespace std; 10 11 int judge(int m) 12 { 13 int p=1; 14 int k=sqrt((double)m); 15 for(int i=2;i<=k;i++){ 16 if(m%i==0){ 17 if(i==k &&k*k==m)p+= i; 18 else p+=i+m/i; 19 } 20 } 21 return p; 22 } 23 24 int main() 25 { 26 int a,b; 27 cin>>a>>b; 28 if(a==1){ 29 printf("1 "); 30 return 0; 31 } 32 double minx=0x7ffff; 33 double p=0; 34 int t; 35 for(int i=b;i>=a;i--){ 36 p=judge(i); 37 if(p==1.0){ 38 t=i; 39 break; 40 } 41 if(minx>p/i){ 42 minx=p/i; 43 t=i; 44 } 45 } 46 printf("%d ",t); 47 return 0; 48 }