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

题意:一个小孩,趁家长不在,拿家里的n个 家具玩,玩了之后放回,而且好坏,一定不是原来的位置(每个都不是),问你有多少种放法

题解:设dp[i]表示  放回i个的方法数,那么  dp[i] = (i-1)*(dp[i-1]+dp[i-2]);

对于第i个数,放在序列的最后一个位置,它的位置一定是正确的,所以一定要和前面i−1个的其中一个交换位置才可以,那么如果选中位置上的物品为错误归放的,即为dp[i−1],如果选中的位置上的物品为正确归放的,即为dp[i−2]

//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
#include<vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll; const int N = +;
const int M = ;
const int inf = 0x3f3f3f3f;
const ll MOD = ; #define MAX_L 20005 //最大长度,可以修改 class bign
{
public:
int len, s[MAX_L];//数的长度,记录数组
//构造函数
bign();
bign(const char*);
bign(int);
bool sign;//符号 1正数 0负数
string toStr() const;//转化为字符串,主要是便于输出
friend istream& operator>>(istream &,bign &);//重载输入流
friend ostream& operator<<(ostream &,bign &);//重载输出流
//重载复制
bign operator=(const char*);
bign operator=(int);
bign operator=(const string);
//重载各种比较
bool operator>(const bign &) const;
bool operator>=(const bign &) const;
bool operator<(const bign &) const;
bool operator<=(const bign &) const;
bool operator==(const bign &) const;
bool operator!=(const bign &) const;
//重载四则运算
bign operator+(const bign &) const;
bign operator++();
bign operator++(int);
bign operator+=(const bign&);
bign operator-(const bign &) const;
bign operator--();
bign operator--(int);
bign operator-=(const bign&);
bign operator*(const bign &)const;
bign operator*(const int num)const;
bign operator*=(const bign&);
bign operator/(const bign&)const;
bign operator/=(const bign&);
//四则运算的衍生运算
bign operator%(const bign&)const;//取模(余数)
bign factorial()const;//阶乘
bign Sqrt()const;//整数开根(向下取整)
bign pow(const bign&)const;//次方
//一些乱乱的函数
void clean();
~bign();
};
#define max(a,b) a>b ? a : b
#define min(a,b) a<b ? a : b bign::bign()
{
memset(s, , sizeof(s));
len = ;
sign = ;
} bign::bign(const char *num)
{
*this = num;
} bign::bign(int num)
{
*this = num;
} string bign::toStr() const
{
string res;
res = "";
for (int i = ; i < len; i++)
res = (char)(s[i] + '') + res;
if (res == "")
res = "";
if (!sign&&res != "")
res = "-" + res;
return res;
} istream &operator>>(istream &in, bign &num)
{
string str;
in>>str;
num=str;
return in;
} ostream &operator<<(ostream &out, bign &num)
{
out<<num.toStr();
return out;
} bign bign::operator=(const char *num)
{
memset(s, , sizeof(s));
char a[MAX_L] = "";
if (num[] != '-')
strcpy(a, num);
else
for (int i = ; i < strlen(num); i++)
a[i - ] = num[i];
sign = !(num[] == '-');
len = strlen(a);
for (int i = ; i < strlen(a); i++)
s[i] = a[len - i - ] - ;
return *this;
} bign bign::operator=(int num)
{
char temp[MAX_L];
sprintf(temp, "%d", num);
*this = temp;
return *this;
} bign bign::operator=(const string num)
{
const char *tmp;
tmp = num.c_str();
*this = tmp;
return *this;
} bool bign::operator<(const bign &num) const
{
if (sign^num.sign)
return num.sign;
if (len != num.len)
return len < num.len;
for (int i = len - ; i >= ; i--)
if (s[i] != num.s[i])
return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
return !sign;
} bool bign::operator>(const bign&num)const
{
return num < *this;
} bool bign::operator<=(const bign&num)const
{
return !(*this>num);
} bool bign::operator>=(const bign&num)const
{
return !(*this<num);
} bool bign::operator!=(const bign&num)const
{
return *this > num || *this < num;
} bool bign::operator==(const bign&num)const
{
return !(num != *this);
} bign bign::operator+(const bign &num) const
{
if (sign^num.sign)
{
bign tmp = sign ? num : *this;
tmp.sign = ;
return sign ? *this - tmp : num - tmp;
}
bign result;
result.len = ;
int temp = ;
for (int i = ; temp || i < (max(len, num.len)); i++)
{
int t = s[i] + num.s[i] + temp;
result.s[result.len++] = t % ;
temp = t / ;
}
result.sign = sign;
return result;
} bign bign::operator++()
{
*this = *this + ;
return *this;
} bign bign::operator++(int)
{
bign old = *this;
++(*this);
return old;
} bign bign::operator+=(const bign &num)
{
*this = *this + num;
return *this;
} bign bign::operator-(const bign &num) const
{
bign b=num,a=*this;
if (!num.sign && !sign)
{
b.sign=;
a.sign=;
return b-a;
}
if (!b.sign)
{
b.sign=;
return a+b;
}
if (!a.sign)
{
a.sign=;
b=bign()-(a+b);
return b;
}
if (a<b)
{
bign c=(b-a);
c.sign=false;
return c;
}
bign result;
result.len = ;
for (int i = , g = ; i < a.len; i++)
{
int x = a.s[i] - g;
if (i < b.len) x -= b.s[i];
if (x >= ) g = ;
else
{
g = ;
x += ;
}
result.s[result.len++] = x;
}
result.clean();
return result;
} bign bign::operator * (const bign &num)const
{
bign result;
result.len = len + num.len; for (int i = ; i < len; i++)
for (int j = ; j < num.len; j++)
result.s[i + j] += s[i] * num.s[j]; for (int i = ; i < result.len; i++)
{
result.s[i + ] += result.s[i] / ;
result.s[i] %= ;
}
result.clean();
result.sign = !(sign^num.sign);
return result;
} bign bign::operator*(const int num)const
{
bign x = num;
bign z = *this;
return x*z;
}
bign bign::operator*=(const bign&num)
{
*this = *this * num;
return *this;
} bign bign::operator /(const bign&num)const
{
bign ans;
ans.len = len - num.len + ;
if (ans.len < )
{
ans.len = ;
return ans;
} bign divisor = *this, divid = num;
divisor.sign = divid.sign = ;
int k = ans.len - ;
int j = len - ;
while (k >= )
{
while (divisor.s[j] == ) j--;
if (k > j) k = j;
char z[MAX_L];
memset(z, , sizeof(z));
for (int i = j; i >= k; i--)
z[j - i] = divisor.s[i] + '';
bign dividend = z;
if (dividend < divid) { k--; continue; }
int key = ;
while (divid*key <= dividend) key++;
key--;
ans.s[k] = key;
bign temp = divid*key;
for (int i = ; i < k; i++)
temp = temp * ;
divisor = divisor - temp;
k--;
}
ans.clean();
ans.sign = !(sign^num.sign);
return ans;
} bign bign::operator/=(const bign&num)
{
*this = *this / num;
return *this;
} bign bign::operator%(const bign& num)const
{
bign a = *this, b = num;
a.sign = b.sign = ;
bign result, temp = a / b*b;
result = a - temp;
result.sign = sign;
return result;
} bign bign::pow(const bign& num)const
{
bign result = ;
for (bign i = ; i < num; i++)
result = result*(*this);
return result;
} bign bign::factorial()const
{
bign result = ;
for (bign i = ; i <= *this; i++)
result *= i;
return result;
} void bign::clean()
{
if (len == ) len++;
while (len > && s[len - ] == '\0')
len--;
} bign bign::Sqrt()const
{
if(*this<)return -;
if(*this<=)return *this;
bign l=,r=*this,mid;
while(r-l>)
{
mid=(l+r)/;
if(mid*mid>*this)
r=mid;
else
l=mid;
}
return l;
} bign::~bign()
{
} bign dp[N];
void init() {
dp[] = ;
dp[] = ;
bign tmp = ;
for(int i=;i<=;i=i+) {
dp[i] = (tmp)*(dp[i-] + dp[i-]);
tmp+=;
}
}
int main() {
init();
int n;
while(scanf("%d",&n)!=EOF) {
if(n==-) break;
cout<<dp[n]<<endl;
}
return ;
}

代码

UVA 10497 - Sweet Child Makes Trouble 高精度DP的更多相关文章

  1. 递推+高精度 UVA 10497 Sweet Child Makes Trouble(可爱的孩子惹麻烦)

    题目链接 题意: n个物品全部乱序排列(都不在原来的位置)的方案数. 思路: dp[i]表示i个物品都乱序排序的方案数,所以状态转移方程.考虑i-1个物品乱序,放入第i个物品一定要和i-1个的其中一个 ...

  2. UVA-10497 Sweet Child Makes Trouble (计数+高精度)

    题目大意:这是一道简单排列组合题 .简单说下题意:n件物品,把这n件物品放到不是原来的位置,问所有的方案数.所有的位置都没有变. 题目解析:按照高中的方法,很快得到一个递推公式:f [n]= (n-1 ...

  3. 容斥原理--计算错排的方案数 UVA 10497

    错排问题是一种特殊的排列问题. 模型:把n个元素依次标上1,2,3.......n,求每一个元素都不在自己位置的排列数. 运用容斥原理,我们有两种解决方法: 1. 总的排列方法有A(n,n),即n!, ...

  4. UVA.357 Let Me Count The Ways (DP 完全背包)

    UVA.357 Let Me Count The Ways (DP 完全背包) 题意分析 与UVA.UVA.674 Coin Change是一模一样的题.需要注意的是,此题的数据量较大,dp数组需要使 ...

  5. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  6. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  7. UVA - 1025 A Spy in the Metro[DP DAG]

    UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...

  8. Hdu 5568 sequence2 高精度 dp

    sequence2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=556 ...

  9. sequence2(高精度dp)

    sequence2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

随机推荐

  1. Windows Phone Listbox虚拟化的问题

    最近在项目里面碰到最头疼的一个难题就是Listbox虚拟化的问题,查阅很多文档其实Listbox本身是支持虚拟化的,那么在什么情况下会破坏Listbox的虚拟化呢?目前我接触到的主要有两个原因:一.是 ...

  2. libevent 定时器示例

    程序执行结果: 每隔2秒,触发一次定时器. (2)98行:evtimer_assign在event.h中定义如下: 再来看看event_assign函数: ev     要初始化的事件对象 base  ...

  3. iOS进阶学习-数据处理之文件读写

    一.沙盒机制 1.什么是沙盒? 每一个iOS应用程序都会为自己创建一个文件系统目录(文件夹),这个独立.封闭.安全的空间,叫做沙盒. 2.沙盒机制(SandBox) 沙盒是一种安全体系. 它规定了应用 ...

  4. MVC 局部加载页面的实例

    我们在做MVC 进行某一块的局部刷新,有的使用AJAX 请求,有的使用局部页: 下面我给大家推荐一种使用局部页面实现的这种方式: 第一步: 嵌套视图页 <div id="showAud ...

  5. VisualSVN Server添加svn项目

    如果你想把一个项目添加到svn的步骤: 第一步:打开VisualSVN Server,在目录Repositries目录下,创建一个文件夹名称: 第二步:要把导入的项目,指定到这个路径:如下图:

  6. Datawarehouse

  7. homework-01 "最大子数组之和"的解决过程

    看到这个题目,我首先想到就是暴力解决 求出所有的子数组的和,取出最大值即可 但其中是可以有优化的 如 子数组[3:6]可以用[3:5]+[6]来计算 即可以将前面的计算结果保留下来,减少后面的重复计算 ...

  8. 带搜索框的下拉框chosen.jQury.js

    下载所需js,css png资源     <link href="chosen.css" rel="stylesheet" type="text ...

  9. asdoc 档案

    1.asdoc air项目会出现无法找到NativeApplication等错误 解决办法:add args   -load-config ....../frameworks/air-config.x ...

  10. Android基础整理之四大组件Activity

    最近准备系统的重新整理复习一下Android的各方面的知识,本着知识分享的原则,我就把梳理过程中一些东西给记录下来,权当一个学习笔记吧. 下面步入正题..... 什么是Activity Activit ...