ural 1118. Nontrivial Numbers

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 ≤ IJ
≤ 106, separated with a space.

Output

Output the only integer N satisfying the following conditions:

  1. INJ;
  2. 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 }

  

时间: 2024-08-24 10:46:10

ural 1118. Nontrivial Numbers的相关文章

递推DP URAL 1586 Threeprime Numbers

题目传送门 1 /* 2 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 3 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 4 所以,dp[i][j][k] 表示i位数字,最高位数字j,第二高位数字k 5 状态转移方程:dp[i][j][k] += dp[i-1][k][l] 6 注意:最高位从1开始枚举:) 7 详细解释:http://blog.csdn.net/zhangyanxing666/article/detai

URAL 2031. Overturned Numbers (枚举)

2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surfing the Internet and came across an interesting puzzle: What is the number under the car? It took some time before Pierre solved the puzzle, but eventually he u

Ural 1586 Threeprime Numbers(DP)

题目地址:Ural 1586 先定义一个prime三维数组来记录素数,若i*100+j*10+k为素数,则标记prime[i][j][k]为1,否则为0.这样对后面的处理很方便. 然后定义一个dp三维数组,dp[n][i][j]表示当前n位的十位数字为i,个位数字为j时的素数个数,这时候状态要从prime[k][i][j]为素数时转移过来,所以状态转移方程为: if(prime[j][k][h])         dp[i][k][h]+=dp[i-1][j][k] 代码如下: #include

ural 1009 K-based Numbers

 1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn't contain two successive zeros. For example: 1010230 is a va

ural 1009. K-based Numbers dp 高精度

点击打开链接 1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn't contain two successive zeros. For example: 1010230 is a

URAL - 1009 - K-based Numbers (简单DP)

1009. K-based Numbers Time limit: 1.0 second Memory limit: 64 MB Let's consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn't contain two successive zeros. For example: 1010230 is a valid

【线性筛】【筛法求素数】【约数个数定理】URAL - 2070 - Interesting Numbers

素数必然符合题意. 对于合数,如若它是某个素数x的k次方(k为某个素数y减去1),一定不符合题意.只需找出这些数. 由约数个数定理,其他合数一定符合题意. 就从小到大枚举素数,然后把它的素数-1次方都排除即可. #include<cstdio> #include<cmath> using namespace std; #define MAXP 1000100 #define EPS 0.00000001 typedef long long ll; ll L,R; bool isNo

URAL 2070 Interesting Numbers(数学)

题目地址:http://acm.timus.ru/problem.aspx?space=1&num=2070 思路:质数一定满足题意(满足条件一,因子数为2为质数).所以只需求出l到r中的合数且因子数为质数的数的个数.该数质因子只能为1(若大于一,则因子数为合数),所以枚举每个质数,若该质数的指数+1(因子数)为质数,则ans--. #include<cstdio> #include<vector> #include<cstring> #include<i

URAL 2070 Interesting Numbers (找规律)

题意:在[L, R]之间求:x是个素数,因子个数是素数,同时满足两个条件,或者同时不满足两个条件的数的个数. 析:很明显所有的素数,因数都是2,是素数,所以我们只要算不是素数但因子是素数的数目就好,然后用总数减掉就好.打个表,找找规律,你会发现, 这些数除外的数都是素数的素数次方,然后就简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include &