Ural 1118. Nontrivial Numbers 求因子和

来源:互联网 发布:virtualbox装centos 编辑:程序博客网 时间:2024/06/06 00:48

1118. Nontrivial Numbers

Time limit: 2.0 second
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:
  1. I ≤ N ≤ J;
  2. N is the least trivial number among the ones that obey the first condition.

Sample

inputoutput
24 28
25
Problem Author: Leonid Volkov
Problem Source: USU Open Collegiate Programming Contest October'2001 Junior Session
#include <iostream>#include <cstdio>using namespace std;int main(){    double ans = 10000000, res, m;    int t, i, j,l, r;    bool flag;    while(cin>>l>>r)    {        flag=true;        if(l == 1)        {            printf("1\n");            continue;        }        if(l == r)        {            printf("%d\n", l);            continue;        }        for(i = r; i >= l; i --)        {            res = 0;            for(j = 2; j*j <= i; j ++)            {                if(i%j == 0)                {                    m = i/j;                    res += m+j;                    if(m == j) res -= m;                }            }            res /= i*1.0;            if(res < ans)            {                ans = res;                t = i;            }            if(res == 0)            {                flag=false;                printf("%d\n", i);                break;            }        }        if(flag)            printf("%d\n", t);    }    return 0;}