LightOJ 1028 Trailing Zeroes (I) (素筛,唯一分解定理,约数个数)

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 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 (10)_2 (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 (T ≤ 10000), denoting the number of test cases.
Each case contains an integer N (1 ≤ N ≤ 1 0 12 10^{12} 1012).

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

Sample Input

3
9
5
2

Sample Output

Case 1: 2
Case 2: 1
Case 3: 1

Note
For 9, the possible bases are: 3 and 9. Since in base 3; 9 is represented as 100, and in base 9; 9 is represented as 10. In both bases, 9 contains a trailing zero.

题意
给出一个十进制数,求有多少种进制表示该数时有后置零,例如9可以用3进制和9进制表示所以是两种

题解
进制基数就是逢几进一,所以给出数的约数即为一个基数。所以题意转化为求给出数的约数个数,但是注意去掉约数1
约数个数公式:
对于 n = p 1 x 1 p 2 x 2 ⋯   p k x k n=p_1^{x_1}p_2^{x_2}\cdots\ p_k^{x_k} n=p1x1p2x2 pkxk
约数个数: ( x 1 + 1 ) ( x 2 + 1 ) ⋯ ( x k + 1 ) = Π i = 1 k ( x i + 1 ) (x_1+1)(x_2+1)\cdots(x_k+1)=\Pi_{i=1}^{k}{(x_i+1)} (x1+1)(x2+1)(xk+1)=Πi=1k(xi+1)

代码

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <unordered_map>
//#include <tr1/unordered_map>

using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y

typedef long long ll;
typedef unsigned long long ull;

const long double INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const double eps = 1e-06;
const long double PI = std::acos(-1);
const int M=32768;
const int maxn = 1e6;

bool isprime[maxn+10];
vector<int> prime;

void euler(){
	isprime[1] = 1;
	for(int i = 2; i <= maxn; ++i){
		if(!isprime[i]) prime.push_back(i);
		for(int j = 0; j < prime.size() && prime[j]*i <= maxn; ++j){
			isprime[prime[j]*i]=1;
			if(i%prime[j] == 0) break;
		}
	}
}

int main() {
    int t,ca=1;
	cin>>t;
	euler();
	while(t--){
		ll n,sum=1;
		cin>>n;
		for(int i = 0; i < prime.size() && n >= (prime[i]*prime[i]); ++i){
			if(n%prime[i]==0){
				int ans=0;
				while(n%prime[i]==0){
					ans++;
					n /= prime[i];
				}
				sum *= ans+1;
			}
		}
		if(n > 1){
			sum *= 2;
		}
		printf("Case %d: %lld\n",ca++,sum-1);
	}
    return 0;
}

/*

*/

Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先选出n范围内的数(范围在10^6即可),然后对n进行因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值