zoukankan      html  css  js  c++  java
  • UVA

    Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an
    interesting number, since it is the first odd number for which the sum of its divisors is larger than the
    number itself.
    To help them search for interesting numbers, you are to write a program that scans a range of
    numbers and determines the number that has the largest number of divisors in the range. Unfortunately,
    the size of the numbers, and the size of the range is such that a too simple-minded approach may take
    too much time to run. So make sure that your algorithm is clever enough to cope with the largest
    possible range in just a few seconds.
    Input
    The first line of input specifies the number N of ranges, and each of the N following lines contains a
    range, consisting of a lower bound L and an upper bound U, where L and U are included in the range.
    L and U are chosen such that 1 ≤ L ≤ U ≤ 1000000000 and 0 ≤ U − L ≤ 10000.
    Output
    For each range, find the number P which has the largest number of divisors (if several numbers tie for
    first place, select the lowest), and the number of positive divisors D of P (where P is included as a
    divisor). Print the text ‘Between L and H, P has a maximum of D divisors.’, where L, H, P,
    and D are the numbers as defined above.
    Sample Input
    3
    1 10
    1000 1000
    999999900 1000000000
    Sample Output
    Between 1 and 10, 6 has a maximum of 4 divisors.
    Between 1000 and 1000, 1000 has a maximum of 16 divisors.
    Between 999999900 and 1000000000, 999999924 has a maximum of 192 divisors.
    题目
    /*
    1.约数个数定理:对于一个数a可以分解质因数:a=a1的r1次方乘以a2的r2次方乘以a3的r3次方乘以……
    
    则a的约数的个数就是(r1+1)(r2+1)(r3+1)……
    
    需要指出来的是,a1,a2,a3……都是a的质因数。r1,r2,r3……是a1,a2,a3……的指数。
    
    2.判断m的约数个数:将m开方得n,判断n之前属于m的约数个数num。若n为整数,则m约数个数为2*num+1,否则为2*num
    */
    #include <bits/stdc++.h>
    
    using namespace std;
    
    int countFactor(int n)
    {
        int cnt = 1;
        for(int i=2; i<=sqrt(n); i++){
            int c = 0;
            while(n % i == 0){
                n /= i;
                c++;
            }
            cnt *= (c + 1);
        }
        if(n > 1) cnt *= 2;
        return cnt;
    }
    
    int main()
    {
        int n, l, u;
    
        scanf("%d", &n);
        while(n--) {
            scanf("%d%d", &l, &u);
    
            int ans = 0,num;
            for(int i=l; i<=u; i++){
                int tmp = countFactor(i);
                if(tmp > ans){
                    ans = tmp;
                    num = i;
                }
            }
    
            printf("Between %d and %d, %d has a maximum of %d divisors.
    ", l, u, num, ans);
        }
    
        return 0;
    }
    Code短除
    #include <iostream>  
    #include <cstdlib>  
      
    using namespace std;  
      
    int visit[34000];  
    int prime[34000];  
      
    //因式分解,计算因子个数   
    int number( int a, int n )  
    {  
        int sum = 1;  
        for ( int i = 0 ; a > 1 && i < n ; ++ i )   
            if ( a%prime[i] == 0 ) {  
                int count = 1;  
                while ( a%prime[i] == 0 ) {  
                    count ++;  
                    a /= prime[i];  
                }  
                sum *= count;  
            }  
        return sum;  
    }  
      
    int main()  
    {  
        //利用筛法计算素数,打表   
        for ( int i = 0 ; i < 34000 ; ++ i )  
            visit[i] = 1;  
        int count = 0;  
        for ( int i = 2 ; i < 34000 ; ++ i )  
            if ( visit[i] ) {  
                prime[count ++] = i;  
                for ( int j = 2*i ; j < 34000 ; j += i )  
                    visit[j] = 0;  
            }  
          
        long a,b,c;  
        while ( cin >> c )  
        while ( c -- ) {  
            cin >> a >> b;  
            long save = a,max = 0,temp;  
            for ( long i = a ; i <= b ; ++ i ) {  
                temp = number( i, count );  
                if ( temp > max ) {  
                    max = temp;  
                    save = i;  
                }  
            }  
              
            cout << "Between " << a << " and " << b << ", " << save  
                 << " has a maximum of " << max << " divisors.
    ";  
        }  
        return 0;  
    }  
    筛法
  • 相关阅读:
    ubuntu 12.04 install flash for firefox
    ubuntu 12.04 修改 grub 启动参数
    ubuntu 12.04 英文系统怎么调出 ibus输入法
    Ubuntu 12.04 临时禁用和启动面板
    git部署
    php 递归删除文件夹
    一‘php文件系统
    xml的解构与组装
    memcached的部署
    1,秒杀系统的设计
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8800989.html
Copyright © 2011-2022 走看看