UVA 294 求约数的个数

#include<iostream>
#include<string>
#include<string>
#include<string.h>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<vector>
#include<stdlib.h>
#include<algorithm>
using namespace std;
long long primes[34000];
 int main(){
     memset(primes,0,sizeof(primes));
     primes[0]=2;
     long long t=1;int flag=1;
     for(long long i=3;i<34000;i++){
            flag=1;
        for(int j=2;j<=sqrt(i);j++){
            if(i%j==0){
                flag=0;
                break;
            }
        }
        if(flag) primes[t++]=i;
     }
     int a1;long long l,u,sum,maxn,k,x,r;
     cin>>r;
     while(r--){
        cin>>l>>u;
        sum=1;
        maxn=0;
        for(long long i=l;i<=u;i++){
            sum=1;x=i;flag=0;
           for(int j=0; j<t;j++){
            if(x%primes[j]==0){
                    a1=0;
                while(x%primes[j]==0){
                    a1++;
                    x=x/primes[j];

                }
                sum*=(a1+1);
            }
             if(x==1) break;
           }

            if(sum>maxn) {maxn=sum;k=i;}
        }
        printf("Between %lld and %lld, %lld has a maximum of %d divisors.\n",l,u,k,maxn);
     }

   return 0;
}
时间: 2024-08-02 07:01:05

UVA 294 求约数的个数的相关文章

高效率求约数的个数

高效率求约数的个数 求约数的个数方法有很多,你可以使用O(n)的方法来遍历看是否为约数,更可以使用复杂度的算法从1~来判断个数,但是在这里我们给出的是一种稍快于的方法,大概节省2/3左右的时间. 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1000) 接下来的1行包括N个整数,其中每个数的范围为(1<=Num<=1000000000) 当N=0时输入结束. 输出: 可能有多组输入数据,对于每组输入数据, 输出N行,其中每一行对应上面

Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP

题目链接:点击打开链接 题意: 给定一个字符串str 求字符串str的 循环节个数为 1-len 个的 最长子串长度 思路:套用kmp的性质 #include<string.h> #include<stdio.h> #include <iostream> using namespace std; #define n 1300 void getnext(char str[n],int next[n]){ int m=strlen(str); next[0]=next[1]

数论考试题(b) 求约数的约数的最大个数

题意:求1~n范围里约数的约数的个数加起来最多的是哪个数 及其总数 题解: /* 对一个数质因数分解 首先要知道两个公式:约数的个数的公式,和约数的约数的个数的公式(详见题解) 然后发现:质因数分解后 小的次冥尽量大 会使答案更优 然后使次幂单调不升 dfs求r序列 */ #include<bits/stdc++.h> using namespace std; #define ll long long int pri[]={2,3,5,7,11,13,17,19,23,29,31,37,41,

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

HDU 5211 筛法求约数

给出n个数a1,a2...an,定义函数 f[i]=j,(i<j),表示aj mod ai=0 的最小j,其中j大于i,如果不存在这样的数,则f[i]=0 求n个数所有f[]值的和 先用筛法o(nlogn)求出每个数的约数 然后每读入一个数x,先找出所有的约数,再看看之前有没有出现过这些约数. 这个回看的过程可以用一个数组维护. 维护watch[],watch[aj]=j 表示aj还没有找到函数值,如果aj是x的约数,那么说明aj的函数值为x的位置. #include<cstdio> #

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+

约数的个数

约数的个数 Description mmoaay小侄子今年上初中,老师出了一道求约数个数的题目,比如8的约数有1,2,4,8共4个. 当数比较小的时候可以人工算,当n较大时就难了. mmoaay嫌麻烦,现在让你编个程序来算. Input 一行一个整数.最后以0结束. Output 分别求出这些整数的约数个数,最后的0不用处理. Sample Input 81000 Sample Output 4 9 题解:求正整数378000共有多少个正约数? 解:将378000分解质因数378000=2^4×

hdu1492(一个数的所有humber bunber的约数的个数)

题目意思: 求一个数的所有humber bunber的约数的个数 http://acm.hdu.edu.cn/showproblem.php?pid=1492 题目分析: 求出2,3,5,7的所有个数,用他们能组合出多少的数,就是n的Humber number数 AC代码: /** *类似求素数的个数每一个数都可以表示成一些素数的乘积 *对于给定的n,需要求出n包含多少的2,3,5,7;用这些个数相乘即为n的Humber number */ #include<iostream> #includ

hdu 6069 Counting Divisors(求因子的个数)

Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 3170    Accepted Submission(s): 1184 Problem Description In mathematics, the function d(n) denotes the number of divisors of