5138:暴力比打表快= =
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 100555;
vector <int> v[N];
int a[5] = {
1, 2, 4, 7, 15
};
void init()
{
for( int i = 1; i <= 100005; i++ )
{
v[i].clear();
}
for( int i = 1; i <= 100005; i++ )
{
for( int j = 0; j < 5; j++)
if(i - a[j] > 0)
v[i].push_back(i-a[j]);
}
}
int main()
{
init();
int n;
while(~scanf("%d", &n))
{
printf("%d", v[n][v[n].size()-1]);
for( int i = v[n].size()-2; i >= 0; i-- )
printf(" %d", v[n][i]);
puts("");
}
return 0;
}
5139:预处理,1KW分成1KK个区间(1~10, 11~20....)然后把XXXXD这种形式的数通过XXXX0这个值来计算,例如f(16516),因为预处理出f(15610)了,于是计算就是O(1)了。预处理是时间1KW,可以接受。
#include <iostream>
#include <stdio.h>
using namespace std;
typedef __int64 LL;
const int size = 10000010;
const int mod = 1000000007;
LL n;
int f[1000010];
int g[1000010];
void init( )
{
f[0] = g[0] = 1;
int now , next;
now = 1;
int sum , ans;
sum = 1;
for( int i = 2 ; i<=size-9 ; i++ )
{
next = (int)( ( 1LL * ( now%mod ) * i ) % mod);
ans = (int)( ( 1LL * (sum%mod) * (next%mod) ) % mod);
now = next;
sum = ans;
if( i%10==0 )
{
f[i/10] = ans;
g[i/10] = next;
}
}
}
int main()
{
init( );
LL temp , sum , ans , now , next;
while( ~scanf("%d",&n) )
{
if( n%10==0 )
{
printf( "%d\n",f[n/10] );
}
else
{
temp = n/10;
ans = 1LL * f[n/10];
sum = ans;
now = 1LL * g[n/10];
for( LL i = temp*10+1 ; i<=n ; i++ )
{
next = (int)( ( now%mod ) * i % mod );
ans = (int)( (sum%mod) * (next%mod) % mod );
now = next;
sum = ans;
}
printf( "%I64d\n",ans);
}
}
return 0;
}