lightoj 1028

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
" , ans , sum);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/TnT2333333/p/6066918.html