Big Number(简单数学题)

本文介绍了一种通过对数来计算给定整数阶乘位数的方法,并提供了相应的AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26003    Accepted Submission(s): 11810


Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
 

Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 10 7 on each line.
 

Output
The output contains the number of digits in the factorial of the integers appearing in the input.
 

Sample Input
  
2 10 20
 

Sample Output
  
7 19
 

Source
Asia 2002, Dhaka (Bengal)

意解:用对数来求n的阶乘的位数;
AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
typedef long long ll;

int main()
{
    int n,m;
    cin>>m;
    while(m--)
    {
        scanf("%d",&n);
        double sum = 0;
        for(int i = 1; i <= n; i++)
        {
            sum += log10((double)i);
        }
        printf("%d\n",(int)sum + 1);
    }
    return 0;
}


### 使用 Python 编程解决古代数学问题 #### 一、二鼠打洞问题 这是一个经典的中国古代数学问题,描述如下:两鼠分别从两端向中间挖通一条长度为 $L$ 的墙。大鼠每天前进的速度增加一倍,而小鼠每天前进的速度减少一半。求它们相遇所需的天数以及各自走过的距离。 以下是基于循环实现的解决方案: ```python def two_mice_hole(length): big_mouse_speed = 1 # 大鼠初始速度 small_mouse_speed = 0.5 # 小鼠初始速度 days = 0 # 记录天数 total_distance_big = 0 # 大鼠总路程 total_distance_small = 0 # 小鼠总路程 while True: days += 1 total_distance_big += big_mouse_speed total_distance_small += small_mouse_speed if (total_distance_big + total_distance_small) >= length: break # 更新速度 big_mouse_speed *= 2 small_mouse_speed /= 2 remaining_length = length - (total_distance_big + total_distance_small) last_day_contribution_big = big_mouse_speed * (remaining_length / (big_mouse_speed + small_mouse_speed)) last_day_contribution_small = small_mouse_speed * (remaining_length / (big_mouse_speed + small_mouse_speed)) total_distance_big += last_day_contribution_big total_distance_small += last_day_contribution_small return days, round(total_distance_big, 2), round(total_distance_small, 2) result = two_mice_hole(100) print(f"Days: {result[0]}, Big Mouse Distance: {result[1]}, Small Mouse Distance: {result[2]}") ``` 上述程序通过模拟每一天两只老鼠的移动情况,最终得出它们相遇所需的时间及其各自的行走距离[^1]。 --- #### 二、李白买酒问题 该问题是关于李白饮酒的一个趣味题目,具体描述为:李白提着一瓶酒出门游玩,在路上遇到一家酒店,他喝了一半又加满;接着再往前走一段路,再次喝酒并加满……如此反复多次后回到家中,瓶中恰好还剩下原来的量。求最初瓶子中的酒有多少升? 下面是利用循环解决问题的方法: ```python def li_bai_drinking(final_volume=8): # 假设最后剩余体积为8单位 volume = final_volume steps = 0 while volume != 0: steps += 1 before_addition = volume * 2 # 加入前的状态恢复 volume = before_addition / 2 # 饮用后的状态还原 initial_volume = volume * 2 ** steps return initial_volume initial_amount = li_bai_drinking() print(f"The initial amount of wine was {initial_amount} liters.") ``` 这段代码逆向推导出最初的酒量,并假设每次操作都遵循相同的逻辑模式[^2]。 --- #### 三、中国剩余定理问题 这是另一个著名的古代数学难题,《孙子算经》提到的内容可以转化为现代形式表示为联立同余方程组。下面展示如何借助暴力枚举法找到满足条件的结果: ```python def chinese_remainder_theorem(): for num in range(1, 1000): # 枚举可能解的空间范围 if num % 3 == 2 and num % 5 == 3 and num % 7 == 2: return num solution = chinese_remainder_theorem() print(f"The smallest number satisfying the conditions is {solution}.") ``` 这里采用简单的遍历策略逐一验证候选数值是否符合条件,从而得到最小正整数解[^3]。 --- ### 总结 以上三个例子展示了不同类型的古典数学趣题可以通过编写简洁高效的 Python 脚本来加以解答。每种算法均体现了特定的设计思路与技巧应用价值所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值