#D 阶乘约数
本题总分:10 分
问题描述
定义阶乘 n ! = 1 × 2 × 3 × ⋅ ⋅ ⋅ × n n! = 1 × 2 × 3 × · · · × nn!=1×2×3×⋅⋅⋅×n。
请问 100 ! 100!100! (100 100100 的阶乘)有多少个约数。
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
39001250856960000
calcCode:
- import java.math.BigInteger;
- public class Test {
- public static void main(String[] args) {
- BigInteger num = BigInteger.ONE;
- for (int i = 1; i <= 100; i++)
- num = num.multiply(new BigInteger(String.valueOf(i)));
- long res = 1;
- for (BigInteger i = new BigInteger("2"); i.multiply(i).compareTo(num) <= 0; i = i.add(BigInteger.ONE)) {
- long cnt = 1;
- while (num.mod(i).compareTo(BigInteger.ZERO) == 0) {
- num = num.divide(i);
- cnt++;
- }
- if (cnt > 1)
- res *= cnt;
- }
- if (num.compareTo(BigInteger.ONE) > 0) res <<= 1;
- System.out.println(res);
- }
- }
算术基本定理求约数个数
再给你们一个模板
- public class Test {
- public static void main(String[] args) { System.out.println(factors(100)); }
- static int factors(long n) {
- int res = 1, now;
- for (int i = 2; i * i <= n; i++) {
- now = 1;
- while (n % i == 0) {
- n /= i;
- now++;
- }
- if (now > 1)
- res *= now;
- }
- return n > 1? res << 1 : res;
- }
- }
最近在学C,C的标准库中没大整形,所以就变换出了这种写法
- #include <stdio.h>
- #include <string.h>
- int main() {
- int factor[100];
- long long res = 1;
- memset(factor, 0x00, sizeof(factor));
- for (int i = 100, n = 100; i; n = --i)
- for (int k = 2; k <= n; k++)
- while (!(n % k)) {
- factor[k]++;
- n /= k;
- }
- for (int i = 0; i < 100; i++)
- res *= factor[i] + 1;
- printf("%lld", res);
- }
以上是《Java 蓝桥杯 国赛 第十一届 C组 试题D:阶乘约数》的全部内容,
感谢您对程序员阿鑫博客的支持!
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!
说道:大佬,为什么最后还要res<<=1啊?