UVA 294 - Divisors 因子个数

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.

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 Land an upper bound U, where L and U are included in the range. L and U are chosen such that  and  .

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 HP has a maximum of Ddivisors.‘, where LHP, and D are the numbers as defined above.

3
1 10
1000 1000
999999900 1000000000
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.

题意:给你  a,b,让你找出 a,b之间因子最多的数是多少  b-a《=1000 我们枚举就好了

//meek
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include<map>
#include<queue>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair

const int N=550;
const ll INF = 1ll<<61;
const int inf = 1000000007;
const int MOD =   2000000011;

ll cal(ll x) {
    ll hav = 0;
   for(ll i = 1; i*i <= x; i++) {
    if(x % i == 0) hav ++;
    if(x % i == 0 && x / i != i) hav++;
   }
   return hav;
}
int main() {
    int T;
    ll a,b,ans,tmp;
    scanf("%d",&T);
    while(T--) {
        ans = -1;
        scanf("%lld%lld",&a,&b);
        for(ll i = a; i <= b; i++) {
            ll temp = cal( i );
            if(temp > ans) {
                ans = temp;
                tmp = i;
            }
        }
        printf("Between %lld and %lld, %lld has a maximum of %lld divisors.\n",a,b,tmp,ans);
    }
    return 0;
}

代码

时间: 2024-08-14 10:56:37

UVA 294 - Divisors 因子个数的相关文章

uva 294 - Divisors(枚举+计数)

题目连接:uva 294 - Divisors 题目大意:给出一个范围L~U,问说在该范围中因子数最多的数是多少. 解题思路:枚举L~U中的数,将数分解成质因子,利用乘法原理求总因子数. #include <cstdio> #include <cstring> #include <cmath> int countFactor (int x) { int ans = 1; int m = sqrt(x+0.5); for (int i = 2; i <= m; i+

UVA 294 Divisors( 因子分解)

294 DivisorsMathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be aninteresting number, since it is the rst odd number for which the sum of its divisors is larger than thenumber itself.To help them search f

UVa 294 - Divisors

计算一个给定区间中因数最多的数. 分析:数论.组合数学.题目的数据的比较大,如果暴力一定会超时,那么就考虑利用其他方法求解. 我们将给定数字因式分解,那么因数的个数就是π(各质因子数+1).(每个质因子取0~上限个) 因为数据时在10^9之内,所以质因数只能是33333以内的素数,利用筛法将素数打表计算即可. #include <iostream> #include <cstdlib> using namespace std; int visit[34000]; int prime

POJ2992 Divisors(因子个数)

题意:给n和k,求组合C(n,k)的因子个数. 这道题,若一开始先预处理出C[i][j]的大小,再按普通方法枚举2~sqrt(C[i][j])来求解对应的因子个数,会TLE.所以得用别的方法. 在说方法前,先说一个n!的性质:n!的素因子分解中的素数p的个数为n/p+n/(p^2)+...+n/(p^k)+... <ACM-ICPC程序设计系列 数论及应用>上的方法,200+ms:首先先求解435以内的素因子.然后预处理出j!中每个素因子的个数,公式如下:num[j][i]=j/prime[i

UVa 294 (因数的个数) Divisors

题意: 求区间[L, U]的正因数的个数. 分析: 有这样一条公式,将n分解为,则n的正因数的个数为 事先打好素数表,按照上面的公式统计出最大值即可. 1 #include <cstdio> 2 #include <cmath> 3 4 const int maxn = 31700; 5 bool vis[maxn + 10]; 6 int prime[3450], cnt = 0; 7 8 void Init() 9 { 10 int m = sqrt(maxn + 0.5);

UVA - 294 Divisors (约数)(数论)

题意:输入两个整数L,U(1<=L<=U<=109,U-L<=10000),统计区间[L,U]的整数中哪一个的正约数最多.如果有多个,输出最小值. 分析: 1.求一个数的约数,相当于分解质因子. 2.例如60 = 2 * 2 * 3 * 5.对于2来说,可选0个2,1个2,2个2,有3种情况,同理对于3,有2种情况,对于5,有2种情况,所以3 * 2 * 2则为60的约数个数. 3.L到U扫一遍,取最大值即可. #pragma comment(linker, "/STAC

UVA 294 294 - Divisors (数论)

UVA 294 - Divisors 题目链接 题意:求一个区间内,因子最多的数字. 思路:因为区间保证最多1W个数字,因子能够遍历区间.然后利用事先筛出的素数求出质因子,之后因子个数为全部(质因子的个数+1)的积 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 35005; int prime[N], pn = 0, v

POJ 2992 Divisors 求组合数因子个数

题目来源:POJ 2992 Divisors 题意:... 思路:素数分解的唯一性 一个数可以被分解成若干素数相乘 p1^x1*p2^x2*...*pn^xn 根据乘法原理 因子数为 (x1+1)*(x2+1)*...*(xn+1) 不能直接求出组合数 会溢出 也不能把每个乘的数分解因子 这样会超时 C(N,M)=N!/(M!*(N-M)!) 另dp[i][j] 代表为i的阶乘中j因子的个数(j是素数) 那么i素数的个数为dp[n][i]-dp[m][i]-dp[n-m][i] 最后for循环从

Almost All Divisors(求因子个数及思维)

---恢复内容开始--- We guessed some integer number xx. You are given a list of almost all its divisors. Almost all means that there are all divisors except 11and xx in the list. Your task is to find the minimum possible integer xx that can be the guessed nu