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 of triviality
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 |
Problem Author: Leonid Volkov
Problem Source: USU Open Collegiate Programming Contest October‘2001 Junior Session
题目描述:在区间a <= k <= b, triviality(k) = (Σ(k的因子) - k)/k, 求k使得triviality(k)最小
思路:(1)如果a = 1, 最小的是1,triviality(1) = 0 ;
(2)如果区间内有素数, 那么取值当然是最大的素数了;
(3)打个素数表暴力判断。。
1 #include <iostream> 2 #include <sstream> 3 #include <fstream> 4 #include <string> 5 #include <vector> 6 #include <deque> 7 #include <queue> 8 #include <stack> 9 #include <set> 10 #include <map> 11 #include <algorithm> 12 #include <functional> 13 #include <utility> 14 #include <bitset> 15 #include <cmath> 16 #include <cstdlib> 17 #include <ctime> 18 #include <cstdio> 19 #include <string> 20 using namespace std; 21 int N, T; 22 const int M = 1e6+5; 23 bool a[M]; 24 int prime[M]; 25 int cnt = 0; 26 void init() { 27 for(int i = 2; i < M; i++) a[i] = true; 28 for(int i = 2; i < M; i++) { 29 if(a[i]) { 30 cnt++; 31 prime[cnt] = i; 32 } 33 for(int j = 1; j <= cnt; j++) { 34 if(i * prime[j] >= M) break; 35 a[i*prime[j]] = false; 36 if(i % prime[j] == 0) break; 37 } 38 } 39 } 40 int main() { 41 //freopen("in.txt", "r", stdin); 42 init(); 43 int r, l; 44 scanf("%d%d", &l, &r); 45 double s = 1e9; 46 int res = 0; 47 for(int k = r; k >= l; k--) { 48 if(a[k]) { 49 res = k; 50 break; 51 } 52 } 53 if(l == 1) printf("1\n"); 54 else if(res > 0) printf("%d\n", res); 55 else { 56 for(int k = l; k <= r; k++) { 57 double sum = 1; 58 int i; 59 for(i = 2; i*i < k; i++) { 60 if(k%i == 0) { 61 sum += i + k/i; 62 } 63 } 64 if(i*i == k) sum += i; 65 if(sum/k < s) { 66 s = sum/k; 67 res = k; 68 } 69 } 70 printf("%d\n", res); 71 } 72 return 0; 73 }