题目
思路①
用1078. 高次方数的尾数的代码修改一下理论上可以过,但是注意到N 可以取到 1e8,用大数据绝对超时,,,
题目分类是大数据结果用大数据过TLE - -|||
代码
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
struct BIG
{
int arr[(int)1e5];
int arr_num;
int power;
int base[100];
int exa;
int len;
BIG(ll a, int b, int c )
{
memset(arr,0,sizeof(arr));
memset(base,0,sizeof(base));
arr[0] = 1;
arr_num = 1;
power = b;
exa = c;
len = 0;
while(a)
{
int temp = a % 10;
base[len++] = temp;
a /= 10;
}
}
void POW()
{
if(!exa)
{return ;}
for(int i = 0; i < power; i++)
{
ll temp[10000];
memset(temp,0,sizeof(temp));
for(int j = 0; j < arr_num; j++)
{
for(int k = 0; k < len; k++)
{
temp[j + k] += arr[j] * base[k];
}
}
for(int k = 0; k < arr_num + len;k++)
arr[k] = temp[k];
carry();
}
}
void carry()//进位
{
arr_num += len - 1;
for(int i = 0; i < arr_num - 1; i++)
{
if(arr[i] >= 10)
{
arr[i+1] += arr[i] / 10;
arr[i] %= 10;
}
}
while(arr[arr_num - 1] >= 10)
{
arr[arr_num] += arr[arr_num - 1] / 10;
arr[arr_num - 1] %= 10;
arr_num++;
}
//test(1);
}
void OUT()
{
// for(int i = exa - 1; i >= 0; i--)
// cout << arr[i];
//test(1);
cout << arr[arr_num - 1]<<endl;
}
//
void test(int q)//用于检验思路是否正确
{
cout << q << endl;
cout << " base ";
for(int i = len - 1; i >= 0; i--)
cout << base[i];
cout << endl << " arr ";
for(int i = arr_num - 1; i >= 0; i--)
cout << arr[i];
cout << endl << "power " << power <<endl << " exa " << exa << endl << " len " << len << endl;
}
};
int main()
{
int t;
while(cin >> t)
{
if(!t)
return 0;
ll a, b, c;
a = t;
b = a;
c = 1;
BIG num(a,b,c);
num.POW();
num.OUT();
}
return 0;
}
思路②
由初等数学知识可知
n
n
=
1
0
n
l
g
n
n^n =10^{nlgn}
nn=10nlgn
可以把nlgn拆分成整数部分和小数部分
显然
10^【nlgn】的首位一定是1,所以第一位数字的关键是他的小数部分
1 0 n l g n = 1 0 [ n l g n ] × 1 0 n l g n − [ n l g n ] 10^{nlgn}=10^{[nlgn]} ×10^{nlgn-[nlgn]} 10nlgn=10[nlgn]×10nlgn−[nlgn]
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a;
while(cin >> a)
{
if(!a)
return 0;
double f = a*log10(a);
f -= (int)f;
int b = pow(10,f);
cout << b << endl;
}
}