Problem D. Dinner Problem
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100342/attachments
Description
A group of k students from Cooking University living in the campus decided that each day of the semester one of them will prepare the dinner for the whole company. The semester lasts for n days.
In sake of fairness they decided that each of the students must prepare the dinner at least once during the semester. Now they wonder how many ways are there to plan the semester — to decide for each day which student would make a dinner that day. Help them to find that out.
Input
The input file contains two integer numbers k and n (1 ≤ k ≤ n ≤ 100).
Output
Output one integer number — the number of different nice paintings of the cottages.
Sample Input
2 3
Sample Output
6
HINT
题意
有n天,有k个人,安排这k个人做饭,问你有多少种安排方案,每个人至少得做一天饭
题解:
简单DP,但是会爆longlong,要用高精度来搞一搞……
java不会真是太伤心了= =
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std; const int MAXN = ; struct bign
{
int len, s[MAXN];
bign ()
{
memset(s, , sizeof(s));
len = ;
}
bign (int num) { *this = num; }
bign (const char *num) { *this = num; }
bign operator = (const int num)
{
char s[MAXN];
sprintf(s, "%d", num);
*this = s;
return *this;
}
bign operator = (const char *num)
{
for(int i = ; num[i] == ''; num++) ; //去前导0
len = strlen(num);
for(int i = ; i < len; i++) s[i] = num[len-i-] - '';
return *this;
}
bign operator + (const bign &b) const //+
{
bign c;
c.len = ;
for(int i = , g = ; g || i < max(len, b.len); i++)
{
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
}
bign operator += (const bign &b)
{
*this = *this + b;
return *this;
}
void clean()
{
while(len > && !s[len-]) len--;
}
bign operator * (const bign &b) //*
{
bign c;
c.len = len + b.len;
for(int i = ; i < len; i++)
{
for(int j = ; j < b.len; j++)
{
c.s[i+j] += s[i] * b.s[j];
}
}
for(int i = ; i < c.len; i++)
{
c.s[i+] += c.s[i]/;
c.s[i] %= ;
}
c.clean();
return c;
}
bign operator *= (const bign &b)
{
*this = *this * b;
return *this;
}
bign operator - (const bign &b)
{
bign c;
c.len = ;
for(int i = , g = ; i < len; i++)
{
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= ) g = ;
else
{
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
bign operator -= (const bign &b)
{
*this = *this - b;
return *this;
}
bign operator / (const bign &b)
{
bign c, f = ;
for(int i = len-; i >= ; i--)
{
f = f*;
f.s[] = s[i];
while(f >= b)
{
f -= b;
c.s[i]++;
}
}
c.len = len;
c.clean();
return c;
}
bign operator /= (const bign &b)
{
*this = *this / b;
return *this;
}
bign operator % (const bign &b)
{
bign r = *this / b;
r = *this - r*b;
return r;
}
bign operator %= (const bign &b)
{
*this = *this % b;
return *this;
}
bool operator < (const bign &b)
{
if(len != b.len) return len < b.len;
for(int i = len-; i >= ; i--)
{
if(s[i] != b.s[i]) return s[i] < b.s[i];
}
return false;
}
bool operator > (const bign &b)
{
if(len != b.len) return len > b.len;
for(int i = len-; i >= ; i--)
{
if(s[i] != b.s[i]) return s[i] > b.s[i];
}
return false;
}
bool operator == (const bign &b)
{
return !(*this > b) && !(*this < b);
}
bool operator != (const bign &b)
{
return !(*this == b);
}
bool operator <= (const bign &b)
{
return *this < b || *this == b;
}
bool operator >= (const bign &b)
{
return *this > b || *this == b;
}
string str() const
{
string res = "";
for(int i = ; i < len; i++) res = char(s[i]+'') + res;
return res;
}
}; istream& operator >> (istream &in, bign &x)
{
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream &out, const bign &x)
{
out << x.str();
return out;
} int n , k ;
bign dp[][]; bign dfs(int i ,int j)
{
if (dp[i][j] != -) return dp[i][j];
if (i > j)
{
return dp[i][j] = ;
}
else if(i == && j == ) return dp[i][j] = ;
else if(i == ) return dp[i][j] = dfs(i,j-) * k;
else return dp[i][j] = dfs(i-,j-) * i + dfs(i,j-) * (k-i);
} int main()
{
freopen("dinner.in","r",stdin);
freopen("dinner.out","w",stdout);
cin >> k >> n;
for(int i = ; i <= ; ++ i)
for(int j = ; j <= ; ++ j)
dp[i][j] = -;
bign ans = dfs(k,n);
cout << ans << endl;
return ;
}
Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度的更多相关文章
-
Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
-
CodeForces Gym 100685J Just Another Disney Problem (STL,排序)
题意:给定你大小未知的n个数,你允许有不超过一万次的询问,每次询问两个数,第i个数是否比第j个数小?然后后台会返回给你一个结果YES或者NO(即一行输入), 然后经过多次询问后,你需要给出一个正确的原 ...
-
Codeforces Gym 100338B Geometry Problem 计算几何
Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
-
Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
-
Educational Codeforces Round 40 F. Runner&#39;s Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
-
[CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
-
Problem : 1022 ( Train Problem I )
做题思路必须很清晰啊....可以用数组存储in或out来着,第一次C++用string啊,效果还行 Problem : 1022 ( Train Problem I ) Judge Status : ...
-
Codeforces Gym 101252D&;&;floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
-
Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
随机推荐
-
the Zen of Python---转载版
摘自译文学习区 http://article.yeeyan.org/view/legendsland/154430 The Zen of Python Python 之禅 Beautiful is b ...
-
django 项目的文件说明
参见官方教程的mysite项目 mysite--- manage.py db.sqlite3 #数据库文件 mysite--- #项目文件夹 __init__.py settings.py urls. ...
-
FastReport中文网
FastReport中文网 http://www.fastreportcn.com/Article/2.html
-
【转】C盘不能扩展卷怎么回事 C盘扩展卷灰色的解决办法
今天有百事网网友“丅亿页”遇到了这样一个问题:电脑C盘剩余容量太小,在看到百事网的一篇“如何合并磁盘分区 windows7调整分区大小方法”文章后,也想将自己C盘系统盘空间扩大.按照上面文章中介绍的步 ...
-
数组升序排序的方法Arrays.sort();的应用
package com.Summer_0421.cn; import java.util.Arrays; /** * @author Summer * 数组升序排序的方法Arrays.sort();应 ...
-
生产者&;消费者.py
1.最简单的 --生产者消费者 send.py# !/usr/bin/env python3.5# -*- coding:utf-8 -*-# __author__ == 'LuoTianShuai' ...
-
bootstrap4学习—Bootstrap v4.0.0-alpha.6的快速参考
下面为Bootstrap v4.0.0-alpha.6中的代码快速检索地址: 网址:https://hackerthemes.com/bootstrap-cheatsheet/ 在使用bootstra ...
-
记录自己对EventLoop和性能问题处理的一点心得【转】
转自:http://www.cnblogs.com/lanyuliuyun/p/4483384.html 1.EventLoop 这里说的EventLoop不是指某一个具体的库或是框架,而是指一种程序 ...
-
jquery中的ajax方法参数的用法和他的含义:
转自:https://www.cnblogs.com/huiyuantang/p/5458278.html 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.ty ...
-
arrow:让Python的日期与时间变的更好
在处理数据的时候经常会碰见各种时间数据,但因为时间数据的格式不统一,所以导致数据处理的时候有一些麻烦.Python的标准库提供了相应模块,但可用性却不高,也不够人性化.本专栏之前已经有文章介绍过在R中 ...