Problem F
Sweet Child Makes Trouble
Input: standard input
Output: standard output
Time Limit: 8 seconds
Children are always sweet but they can sometimes make you feel bitter. In this problem, you will see how Tintin, a five year’s old boy, creates trouble for his parents. Tintin is a joyful boy and is always busy in doing something. But what he does is not always pleasant for his parents. He likes most to play with household things like his father’s wristwatch or his mother’s comb. After his playing he places it in some other place. Tintin is very intelligent and a boy with a very sharp memory. To make things worse for his parents, he never returns the things he has taken for playing to their original places.
Think about a morning when Tintin has managed to ‘steal’ three household objects. Now, in how many ways he can place those things such that nothing is placed in their original place. Tintin does not like to give his parents that much trouble. So, he does not leave anything in a completely new place; he merely permutes the objects.
Input
There will be several test cases. Each will have a positive integer less than or equal to 800 indicating the number of things Tintin has taken for playing. Each integer will be in a line by itself. The input is terminated by a –1 (minus one) in a single line, which should not be processed.
Output
For each test case print an integer indicating in how many ways Tintin can rearrange the things he has taken.
Sample Input
2
3
4
-1
Sample Output
1
2
9
package main;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static private Scanner cin = new Scanner(System.in);
static private BigInteger[] factorial = new BigInteger[888];
static private BigInteger[][] C = new BigInteger[888][888];
static private BigInteger[] result = new BigInteger[888];
public static void init()
{
factorial[0]=new BigInteger("1");
for(int i=1;i<=800;i++)
factorial[i] = factorial[i-1].multiply(new BigInteger(""+i+""));
for(int i=0;i<=800;i++)
for(int j=0;j<=800;j++)
C[i][j] = new BigInteger("0");
for(int i=0;i<=800;i++)
{
C[i][0]=new BigInteger("1");
for(int j=1;j<=i;j++)
{
C[i][j] = C[i-1][j-1].add(C[i-1][j]);
}
}
result[0] = new BigInteger("0");
result[1] = new BigInteger("0");
result[2] = new BigInteger("1");
for(int i=3;i<=800;i++)
{
result[i] = factorial[i].subtract(new BigInteger("1"));
for(int j=1;j<i-1;j++)
{
result[i] = result[i].subtract(C[i][j].multiply(result[i-j]));
}
}
}
public static void main(String[] args) {
init();
int n;
n = cin.nextInt();
while(n>-1)
{
System.out.println(result[n]);
n = cin.nextInt();
}
}
}
不知神牛又是怎么做的……
和队友讨论了一下,队友找到了一份C++版的解题报告……思想基本如下:
/*
* 大数模版
* 完美支持负数及加减法操作
* 支持乘法,大小比较,赋值
* 支持与long long类型的赋值比较
* SF-_- 14.04.01
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int K = 10000; // 数组里每位代表1W
const int M = 300; // 一共10位
const char show[] = "%04lld";
struct Bignum
{
LL a[M*2]; // 大数数组
int len; // 长度
bool negative; // 正负
Bignum()
{
clear();
}
void clear()
{
len=0;
negative=false;
memset(a, 0, sizeof(a));
}
Bignum(LL num)
{
*this=num;
}
Bignum operator=(LL num)
{
clear();
if(num<0) negative=true, num=-num;
while(num)
a[len++]=num%K,num/=K;
return *this;
}
Bignum(const Bignum& cmp)
{
memcpy(this, &cmp, sizeof(Bignum));
}
Bignum operator=(const Bignum& cmp)
{
memcpy(this, &cmp, sizeof(Bignum));
return *this;
}
int absCmp(const Bignum& cmp)
{
if(len!=cmp.len)
return len>cmp.len?1:-1;
for(int i=len-1;i>=0;i--)
if(a[i]!=cmp.a[i])
return a[i]>cmp.a[i]?1:-1;
return 0;
}
int absCmp(LL num)
{
Bignum cmp(num);
return absCmp(cmp);
}
bool operator<(const Bignum& cmp)
{
if(negative^cmp.negative)
return negative?true:false;
if(negative)
return absCmp(cmp)>0;
else
return absCmp(cmp)<0;
}
bool operator<(LL num)
{
Bignum cmp(num);
return *this<cmp;
}
bool operator==(const Bignum& cmp)
{
if(negative^cmp.negative)
return false;
return absCmp(cmp)==0;
}
bool operator==(LL num)
{
Bignum cmp(num);
return *this==cmp;
}
void absAdd(const Bignum& one, const Bignum& two)
{
len=max(one.len, two.len);
for(int i=0;i<len;i++)
{
a[i]+=one.a[i]+two.a[i];
if(a[i]>=K) a[i]-=K, a[i+1]++;
}
if(a[len]) len++;
}
void absSub(const Bignum& one, const Bignum& two)
{
len=one.len;
for(int i=0;i<len;i++)
{
a[i]+=one.a[i]-two.a[i];
if(a[i]<0) a[i+1]--,a[i]+=K;
}
while(len>0 && a[len-1]==0) len--;
}
void absMul(const Bignum& one, const Bignum& two)
{
len=one.len+two.len;
for(int i=0;i<one.len;i++) for(int j=0;j<two.len;j++)
a[i+j]+=one.a[i]*two.a[j];
for(int i=0;i<len;i++) if(a[i]>=K)
a[i+1]+=a[i]/K,a[i]%=K;
while(len>0 && a[len-1]==0) len--;
}
Bignum operator+(const Bignum& cmp)
{
Bignum c;
if(negative^cmp.negative)
{
bool res = absCmp(cmp)>0;
c.negative = !(negative^res);
if(res)
c.absSub(*this, cmp);
else
c.absSub(cmp, *this);
}
else if(negative)
{
c.negative=true;
c.absAdd(*this, cmp);
}
else
{
c.absAdd(*this, cmp);
}
return c;
}
Bignum operator-(const Bignum& cmp)
{
Bignum cpy;
if(cpy==cmp)
return *this;
else
cpy=cmp, cpy.negative^=true;
return *this+cpy;
}
Bignum operator*(const Bignum& cmp)
{
Bignum c;
if(c==cmp || c==*this)
return c;
c.negative = negative^cmp.negative;
c.absMul(*this, cmp);
return c;
}
void output()
{
if(len==0)
{
puts("0");
return;
}
if(negative)
printf("-");
printf("%lld", a[len-1]);
for(int i=len-2;i>=0;i--)
printf(show, a[i]);
puts("");
}
};
Bignum res[801];
void init()
{
res[1]=0;
res[2]=1;
for(int i=3;i<=800;i++)
res[i] = (res[i-1]+res[i-2])*(i-1);
}
int main()
{
init();
int n;
while(~scanf("%d", &n) && n>=0)
res[n].output();
}
好吧,思维不及大牛也,惭愧,惭愧