LightOJ - 1028 Trailing Zeroes (I)

题意:T(<=1e4)组数据,求对于N(<=1e12),已知N在k进制表示下末位是0,求有多少个可能的k

相当与求解每个N的因数个数减1(代表除去1进制的情况)

N=p1a1*p2a2*...*pnan,若N=m*n,那么m可表示为m=x*p1^(k), 0 <= k <= a1,

那么N的约数个数为(a1+1)(a2+1)...(an+1)

对于不大于1e12的数,若它为素数,答案恒为1;若它不为素数,那么它必然可以被不大于1e6的数整除,所以我们只需要预处理出不大于1e6的素数表即可。

计算时求出每个素数的指数,不断累乘即可。注意最后如果N!=1,说明最后剩下的是素数,所以答案再乘2.代表指数0和1两种情况(类似于求欧拉函数的过程)

复杂度的话,π(1000000)=1000000/ln(1000000)=72383,最坏情况下约7*10^(8),推车.jpg

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define INF 0x3f3f3f3f
 5 #define MOD 1000000007
 6 typedef long long LL;
 7
 8 const int maxn = 1e6 + 10;
 9 int prime[maxn];
10 bool is_prime[maxn];
11
12 int T;
13 LL N;
14
15 void init() {
16     for (int i = 0; i < maxn; i++) is_prime[i] = true;
17     is_prime[0] = is_prime[1] = false;
18     for (int i = 2; i < maxn; i++) {
19         if (is_prime[i]) {
20             prime[++prime[0]] = i;
21             for (int j = i + i; j < maxn; j += i) is_prime[j] = false;
22         }
23     }
24 }
25
26 int main() {
27     init();
28     scanf("%d", &T);
29     for (int t = 1; t <= T; t++) {
30         scanf("%lld", &N);
31         LL ans = 1;
32         for (int i = 1; i <= prime[0] && (LL)prime[i] * prime[i] <= N; i++) {
33             LL tmp = 0;
34             while (N % prime[i] == 0) {
35                 N /= prime[i];
36                 tmp++;
37             }
38             ans *= (tmp + 1);
39         }
40         if (N > 1) ans *= 2;
41         printf("Case %d: %lld\n", t, ans - 1);
42     }
43     return 0;
44 }
时间: 2024-07-30 01:40:24

LightOJ - 1028 Trailing Zeroes (I)的相关文章

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子, 可以用n的所有素因子排列组合而来, n = (a1x1) * (a2 x2) * (a3x3)...*(anxn), 其中ai为n的素因子,那么n的因子的个数等同于(x1 + 1) * (x2 + 1) * (x3 + 1) ... * (xn + 1)中排列, 因为其中一种排列肯定为所有素因子

Lightoj 1090 - Trailing Zeroes (II)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q<=1000000,求出的末尾有几个0? 解题思路: 是不是一下子懵了,数字好大,复杂度好高,精度怎么办···············,就问你怕不怕? 因为你是Acmer,这都不应该是问题.因为10的因子只有2和5,所以可以打表保存从1到当前数字相乘的积中分别含有2,5的个数.然后算出中分别含有2,5的个数,

LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 &amp; 二分

题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出impossible 可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可; LL Find(LL x) { LL ans = 0; while(x) { ans += x/5; x /=

lightoj-1028 - Trailing Zeroes (I)(素数法求因子个数)

1028 - Trailing Zeroes (I) PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBWe know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols

light oj1028 - Trailing Zeroes (I)

1028 - Trailing Zeroes (I) We know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. But in different bases we use differen

LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

Trailing Zeroes (III)(lightoj 二分好题)

1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N.

Trailing Zeroes (III) -;lightoj 1138

Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For ex