江西财经大学第一届程序设计竞赛 H题- 小P的数学问题
题目链接:https://www.nowcoder.com/acm/contest/115/H
解题思路:分块打表!!!
什么是分块打表呢???
从这道题我们知道我们要找到最多1*e9的阶乘
那循环暴力肯定tle,就是不tle,数组也开不了那么大的空间。
那么我们将1——1*e9分为100个区间。即
[ 1,1*e7] 、[1*e7 , 2*e7] 、、、、[99*e7 ,100*e7]。
我们只需要将中间的节点存起来就行,然后每次循环只需要1*e7次就可以了。
AC代码:
1 #include<iostream>
2 #define INF 1000000007
3 #define m 10000000
4 using namespace std;
5 long long a[110]={1,682498929,491101308,76479948,723816384,67347853,27368307,
6 625544428,199888908,888050723,927880474,281863274,661224977,623534362,
7 970055531,261384175,195888993,66404266,547665832,109838563,933245637,
8 724691727,368925948,268838846,136026497,112390913,135498044,217544623,
9 419363534,500780548,668123525,128487469,30977140,522049725,309058615,
10 386027524,189239124,148528617,940567523,917084264,429277690,996164327,
11 358655417,568392357,780072518,462639908,275105629,909210595,99199382,
12 703397904,733333339,97830135,608823837,256141983,141827977,696628828,
13 637939935,811575797,848924691,131772368,724464507,272814771,326159309,
14 456152084,903466878,92255682,769795511,373745190,606241871,825871994,
15 957939114,435887178,852304035,663307737,375297772,217598709,624148346,
16 671734977,624500515,748510389,203191898,423951674,629786193,672850561,
17 814362881,823845496,116667533,256473217,627655552,245795606,586445753,
18 172114298,193781724,778983779,83868974,315103615,965785236,492741665,
19 377329025,847549272,698611116};
20 int main(){
21 int T;
22 cin>>T;
23 while(T--){
24 long long n;
25 cin>>n;
26 int t=n/m;
27 int mx=(t+1)*m;
28 long long ans=a[t];
29 if(n%m==0){
30 cout<<a[t]<<endl;
31 }else{
32 for(int i=t*m+1;i<mx;i++){
33 long long val=ans*i%INF;
34 if(i==n){
35 cout<<val<<endl;
36 break;
37 }
38 ans=val;
39 }
40 }
41 }
42 return 0;
43 }