LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合


题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数。
思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了。但注意是1e12次方法不可行。而一般的求因子显然也太慢,所有要想另一个办法。已知任意数可以分解成几个质因数幂的乘积,所以求出n所有的质因数和它的指数再进行排列组合就可以得到答案了。


#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#define LL long long
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e6+2000;
LL pri[N];
LL vis[N];
LL c = 0;
void prime()
{
    MMF(vis);
    for(LL i = 2; i < N; i++)
    {
        if(!vis[i])
        {
            for(LL j = i*i; j < N; j+= i)
                vis[j] = 1;
            pri[c++] = i;
        }
    }
}

int main()
{
    prime();
    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
        LL n;
        scanf("%lld", &n);
        LL ans = 1;
        for(int i = 0; i < c && pri[i]*pri[i] <= n; i++)
        {
            int ct = 0;
            while(n % pri[i] == 0)
            {
                ct++;
                n /= pri[i];
            }
            ans *= ct+1;
        }
        if(n > 1)//减枝后考虑n为质数的情况
            ans <<= 1;
        printf("Case %d: %lld\n", ++cnt, ans - 1);
    }
    return 0;
}
//可知任意数可分解成(p1^x)(p2^y)…的形式,所以求解因子只要在x、y、z…间排列组合就可以了
//这题无法直接使用欧拉函数打表,1e12的数据量定会超时 

转载于:https://www.cnblogs.com/Yumesenya/p/6008097.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值