lightoj 1028 - Trailing Zeroes (I)(素数筛)

本文探讨了将一个十进制数转换为不同进制数时,在哪些进制下该数末位会出现0的问题。通过分析数的质因数分解,提出了一种高效的算法来计算可能的进制数量。

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 different symbols. For example in binary number system we use only 0 and 1. Now in this problem, you are given an integer. You can convert it to any base you want to. But the condition is that if you convert it to any base then the number in that base should have at least one trailing zero that means a zero at the end.

For example, in decimal number system 2 doesn't have any trailing zero. But if we convert it to binary then 2 becomes (10)2 and it contains a trailing zero. Now you are given this task. You have to find the number of bases where the given number contains at least one trailing zero. You can use any base from two to infinite.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer N (1 ≤ N ≤ 1012).

Output

For each case, print the case number and the number of possible bases where N contains at least one trailing zero.

题意:给定一个10进制数n, n <= 10 ^ 12, 问把它转换成哪一些进制的数,这个数末尾会有0。

其实就是问你他的约数个数,由于题中给的组数有点大10的4次直接求因子会超时,所以要换种方法求。

由于每个数都可以化为几个素数的积,所以可以利用这种思想

a=prime1^a1 * prime2^a2 * prime^a3......

sum=(a1 + 1) * (a2 + 1) * (a3 + 1)......

这题还有一些要优化的东西具体优化看一下代码。

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int M = 1e6 + 10;
int prime[M];
int a[M];
bool isprime[M];
int counts;
void getprime() {
    isprime[0] = isprime[1] = false;
    isprime[2] = true;
    for(int i = 3 ; i <= M ; i++) {
        isprime[i] = i % 2 ? true : false;
    }
    int t = (int)sqrt(M * 1.0);
    for(int i = 3 ; i <= t ; i++) {
        if(isprime[i]) {
            for(int j = i * i ; j <= M ; j += i) {
                isprime[j] = false;
            }
        }
    }
    counts = 0;
    for(int i = 2 ; i <= M ; i++) {
        if(isprime[i]) {
            prime[counts++] = i;
        }
    }
}
int main()
{
    int t;
    getprime();
    scanf("%d" , &t);
    int ans = 0;
    while(t--) {
        ans++;
        ll n;
        scanf("%lld" , &n);
        ll sum = 1;
        for(int i = 0 ; (ll)prime[i] * prime[i] <= n ; i++) {
            int flag = 0;
            while(n % prime[i] == 0) {
                n /= prime[i];
                flag++;
            }
            sum *= (flag + 1);
        }
        if(n > 1) {
            sum *= 2;
        }
        sum--;
        printf("Case %d: %lld\n" , ans , sum);
    }
    return 0;
}

转载于:https://www.cnblogs.com/TnT2333333/p/6066918.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值