找规律
f(1)=1
f(2)=1*1*2=(1)*(1*2)=1!*2!
f(3)=1*1*1*2*2*3=(1)*(1*2)*(1*2*3)=1!*2!*3!
式子可以简化为
f(n)=∏i=1n(n!)%MOD
,直接打表不行,会超内存,可以对数据进行离线处理。排好序之后从小到大暴力。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1000005
#define ll __int64
const int mod=1000000007;
struct node
{
int x,id,s;
}a[N];
bool cmp1(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.id<b.id;
}
int main()
{
int m=0,i,j,n;
ll s,s2,s3;
while(~scanf("%d",&n))
{
a[m].id=m;
a[m++].x=n;
}
sort(a,a+m,cmp1);
s2=s3=1;
s=1;
for(i=0,j=2; i<m; i++)
{
for(; j<=a[i].x; j++)
{
s=s*j;
if(s>=mod)
s%=mod;
s2=(s*s2)%mod;
s3=s2;
}
a[i].s=s2;
}
sort(a,a+m,cmp2);
for(i=0;i<m;i++)
printf("%d\n",a[i].s);
return 0;
}
管方题解代码参考自http://www.cnblogs.com/walker11/p/4148947.html
==========================================分=割=线====================================================
我的做法:
离线预处理出0 10 100 1000...i的i!值,然后从n/10+1->n开始推。。。
/* BC code
* by qiu
* 12.6
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=10000010;
const long long MOD=1000000007;
long long a[1000010], b[1000010];
int main()
{
long long sum=1, ans=1;
a[0]=1,b[0]=1;
for(int i=1; i<=10000005; i++){
sum = (sum%MOD*i%MOD)%MOD;
ans = (ans%MOD*sum%MOD)%MOD;
if(i%10==0){
b[i/10]=sum;
a[i/10]=ans;
}
}
int n;
while(scanf("%d", &n)!=EOF){
ans=a[n/10];
sum=b[n/10];
printf("%d\n", n/10*10+1);
for(int i=n/10*10+1; i<=n; i++){
sum = (sum%MOD*i%MOD)%MOD;
ans = (ans%MOD*sum%MOD)%MOD;
}
printf("%lld\n", ans);
}
return 0;
}