E - Leading and Trailing(数学)

本文通过一道涉及大数运算的题目,介绍了如何使用对数来解决大数的前导问题。文中详细解释了科学计数法的概念,并通过具体的编程实现展示了如何找到大数的前导数字。

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

这道题的精度把我坑惨了,结果我发现我在printf的时候没有加03,题目明明就说n^k 至少6位,结果我想了 2^1不就是要补0吗,唉,我太菜了!! 但是这道题我学到了 如何利用对数解决大数的前导问题。

在这里插入图片描述
首先应该知道什么是科学表示,虽然小学生都知道但是还是复习一下在这里插入图片描述
在这里插入图片描述
也就是每个数都可以有a10^b表示出来,a代表前面的1.234567,b就是6;
知道了这个然后怎么去找a呢?找到了不就是直接(int)(a
100)了吗,这点就用到了对数log10()了,但是中间推到有一点我不明白就是在求b的时候,姑且我认为是数论结论吧。
步骤:两边同时取对数之后运算可知。。。。。
在这里插入图片描述
当然别以为这样直接带值就OK了,其当时WA了几次就是因为不知道modf()函数,首先介绍一下它的作用:
modf(y,&x);这里是返回y的整数部分,而小数部分给了x。如果你不用函数(反正我直接算的就WA了也不知道为什么)

#include<map>
#include<list>
#include<ctime>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<string>
#include<cstdlib>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
//ll gcd(ll a,ll b){
//   return b?gcd(b,a%b):a;
//}
#define Mod 1000
ll QP(ll x,ll n){
     ll res=1;
     while(n){
       if(n&1){
         res=(res*x)%Mod;
       }
       x=(x*x)%Mod;
       n>>=1;
     }
       return res%Mod;
}
//bool is_prime(int x)
//{
//    if (x < 2 || x % 2 == 0)return false;
//    for (int i = 3; i*i < x;i+=2)
//    if (x%i == 0)return false;
//    return true;
//}
//欧拉函数模板用于求1--n与n互质的数的个数 
//ll eular(ll n){
//	 ll ans=1;
//	 for(ll i=2;i*i<=n;i++)
//	 	if(n%i==0){
//	 		n/=i;ans*=i-1;
//	 		while(n%i==0) n/=i,ans*=i;
//		 }
//	    if(n>1) ans*=n-1;
//	    return ans;
//}
//筛素数模板
//const int N = 1e7 + 10;
//int pri[700010], k;
//bool Isprime[N];
//void prime()
//{
//    k = 0;
//    memset(Isprime, true, sizeof(Isprime));
//    Isprime[1] = false;
//    for(int i = 2 ; i < N ; i++)
//    {
//        if(Isprime[i])
//        {
//            pri[k++] = i;
//            for(int j = 2 ; i * j < N ;j++)
//                Isprime[i * j] = false;
//        }
//    }
//}//素数筛选
int main()
{ 
    ll T,n,k,g=1;
    double x=0;
    scanf("%lld",&T);
    while(T--){
       scanf("%lld %lld",&n,&k);
	   ll tail_ans=QP(n,k)%1000;
	double y=modf((double)(k*log10(n)),&x);
	ll lead=pow(10,y)*100; 
	   printf("Case %lld: %03lld %03lld\n",g++,lead,tail_ans);
	}
  return 0;
}
import os import re def sanitize_filename(filename): # Split filename into name and extension name, ext = os.path.splitext(filename) # Clean the name part, keeping only alphanumeric characters, underscores, and dots sanitized_name = re.sub(r'[^a-zA-Z0-9_.]', '_', name) # Return sanitized name with original extension return sanitized_name + ext def process_file(input_file_path, output_folder): # Ensure the output folder exists if not os.path.exists(output_folder): os.makedirs(output_folder) current_file = None # Current file object being written to # Try different encodings to handle .bat files (ANSI/cp1252 is common for .bat) encodings = ['utf-8', 'cp1252', 'latin1'] # Fallback encodings for encoding in encodings: try: with open(input_file_path, 'r', encoding=encoding) as infile: for line in infile: line = line.strip() # Remove leading/trailing whitespace if line.startswith('WS'): # Close the current file if one is open if current_file is not None: current_file.close() # Create a new filename from the 'WS' line, replacing spaces and colons file_name = f"{line.replace(' ', '_').replace(':', '')}.txt" print(f"Original filename: {file_name}") sanitized_file_name = sanitize_filename(file_name) print(f"Sanitized filename: {sanitized_file_name}") # Construct full file path and open the new file file_path = os.path.join(output_folder, sanitized_file_name) current_file = open(file_path, 'w', encoding='utf-8') # Write fixed header lines current_file.write("SpotUPos = 51\n") current_file.write("MaxGrayPos = 557\n") elif line.startswith('Spot') and current_file is not None: # Process 'Spot' lines only if a file is open # Extract data after 'SpotX:' (e.g., 'Spot1:') match = re.match(r'Spot\d+:(.*)', line) if match: data_line = match.group(1).strip() if data_line: # Ensure there's data to write # Split data by whitespace and write each point on a new line for data_point in data_line.split(): current_file.write(data_point + '\n') break # Exit loop if file is successfully read except UnicodeDecodeError: print(f"Failed to decode with {encoding}, trying next encoding...") continue except Exception as e: print(f"Error processing file: {e}") return # Ensure the last file is closed if current_file is not None: current_file.close() # Example usage with a .bat file input_file = r"D:\A_smee\FLS\data\211016\211016_20250122\B\SFOI\SpotOrigImage_20250122_154534.dat" # Replace with your .bat file path output_dir = r"D:\A_smee\FLS\data\211016\211016_20250122\B\SFOI" process_file(input_file, output_dir) 使用中文注释
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值