CodeForces - 233D

题目大意给你一个n*m 的矩阵,要求你进行涂色,保证每个n*n的矩阵内都有k个点被涂色。

问你一共有多少种涂色方案。 n<=100 && m<=1e18

看数据范围感觉是个矩阵快速幂优化的dp,各种想,连状态转移方程都想不出来,我真

鸡儿菜!!!!,这种和概率有关的dp我感觉好蓝啊!!!

思路:显然是个dp,我们另dp[ i ][ j ]表示,到 i 列,一共涂了j个格子的种数,

那么有状态转移方程 dp[ i ][ j ] +=Σ(dp[ i - 1 ][ j - s ] * c[ n ][ s ] ) ( 0<=s<=j ) c[ i ] [ j ]表示组合数。

但是m的范围是1e18显然不能直接dp,我们观察可以发现,第 i 列 和 第 i + n 列的涂色格子的

个数肯定是一样的,那么我们可以用快速幂把>n的列的种数全部并到<n 的列中,这样就能在

100^3的时间内完成。

 #include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+;
ll c[][],n,m,k,dp[][],res[][][];
void init()
{
c[][]=;
for(int i=;i<=;i++)
{
c[i][]=i;
c[i][i]=c[i][]=;
for(int j=;j<i;j++)
{
c[i][j]=(c[i-][j-]+c[i-][j])%mod;
}
}
}
ll q_pow(ll x,ll k)
{
ll ans=,d=x;
while(k)
{
if(k&) ans=(ans*d)%mod;
d=(d*d)%mod;
k>>=;
}
return ans;
}
int main()
{
init();
cin>>n>>m>>k;
int r=m%n;
for(int i=;i<=n;i++)
{
for(int j=;j<=k;j++)
{
if(j>n) break;
if(i<=r) res[i][j][]=q_pow(c[n][j],m/n+);
else res[i][j][]=q_pow(c[n][j],m/n);
}
}
for(int i=;i<=n;i++) dp[i][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=k;j++)
{
for(int u=;u<=min((ll)j,n);u++)
{
if(i<=r) dp[i][j]=(dp[i][j]+dp[i-][j-u]*res[i][u][])%mod;
else dp[i][j]=(dp[i][j]+dp[i-][j-u]*res[i][u][])%mod;
}
}
}
printf("%I64d\n",dp[n][k]);
return ;
}

Codeforces Round #144 (Div. 2) D table的更多相关文章

  1. 贪心 Codeforces Round #273 (Div. 2) C. Table Decorations

    题目传送门 /* 贪心:排序后,当a[3] > 2 * (a[1] + a[2]), 可以最多的2个,其他的都是1个,ggr,ggb, ggr... ans = a[1] + a[2]; 或先2 ...

  2. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  3. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  4. Codeforces Round #273 (Div. 2)-C. Table Decorations

    http://codeforces.com/contest/478/problem/C C. Table Decorations time limit per test 1 second memory ...

  5. Codeforces Round #144 (Div. 2)

    A. Perfect Permutation 奇偶对调. B. Non-square Equation \(s(x)\)不超过200,根据求根公式计算\(x\). C. Cycles 每次新增点时都和 ...

  6. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

  7. codeforces 的 Codeforces Round #273 (Div. 2) --C Table Decorations

    C. Table Decorations time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #273 (Div. 2)C. Table Decorations 数学

    C. Table Decorations   You have r red, g green and b blue balloons. To decorate a single table for t ...

  9. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. Linux配置Tomcat步骤mv apache-tomcat-7.0.82 tomcat

    (一)安装JAVA1.检查java环境 java -version,不存在安装.2.yum -y list java* Loaded plugins: fastestmirror, langpacks ...

  2. android 获取图库中展示到界面中

    layout: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. python - 类的内置 attr 方法

    类的内置 attr 方法 #类的内置 attr 方法: # __getattr__ # __setattr__ # __delattr__ # __getattr__ #到调用一个类不存在数参数时,将 ...

  4. 【Udacity并行计算课程笔记】- Lesson 3 Fundamental GPU Algorithms (Reduce, Scan, Histogram)

    本周主要内容如下: 如何分析GPU算法的速度和效率 ​​3个新的基本算法:归约.扫描和直方图(Reduce.Scan.Histogram) 一.评估标准 首先介绍用于评估GPU计算的两个标准: ste ...

  5. Freemarker导出带多个不重复图片的word

    1.新建一个word,添加一张图片,调整好图片大小与位置.

  6. css 背景图片自适应元素大小

    一.一种比较土的方法,<img>置于底层. 方法如下: CSS代码: HTML: <img src="背景图片路径" /> <span>字在背景 ...

  7. Linux 调优方案, 修改最大连接数(ulimit命令)【转】

    转自:http://blog.csdn.net/liangxiaozhang/article/details/8363435 Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源 ...

  8. 配置samba文件服务器

    1.打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--> ...

  9. python 运行日志logging代替方案

    以下是自己写的 记录日志的代码.(和logging不搭嘎,如果如要学loggging模块,本文末尾有他人的链接.) # prtlog.py ############################## ...

  10. PYTHON-TCP 粘包

    1.TCP的模板代码 收发消息的循环 通讯循环 不断的连接客户端循环 连接循环 判断 用于判断客户端异常退出(抛异常)或close(死循环) 半连接池backlog listen(5) 占用的是内存空 ...