传送门

分析

先用并查集将所有相等元素连为一个,得到不同的元素共cnt种,之后我们的任务便转化为将这些元素分为k组(k≤cnt),所以我们不难得出dp式:dpij=dpi-1j-1*j+dpi-1j*j

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl;
long long dp[][];int cnt,fa[];
class PossibleOrders{
public:
int sf(int x){return fa[x]==x?x:fa[x]=sf(fa[x]);}
void work(string s){
int i,k=,x=,y=,n=s.length();
for(i=;i<n;i++)
if(s[i]=='=')k++;
else if(!k)x=(x<<)+(x<<)+(s[i]-'');
else y=(y<<)+(y<<)+(s[i]-'');
if(sf(x)!=sf(y)){
cnt--;
fa[sf(x)]=sf(y);
}
return;
}
long long howMany(int num,vector<string>facts){
int i,j,n=num;
long long ans=;
cnt=n;
for(i=;i<n;i++)fa[i]=i;
for(i=;i<facts.size();i++)work(facts[i]);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
dp[i][j]=;
dp[][]=;
for(i=;i<=cnt;i++)
for(j=;j<=cnt;j++)
dp[i][j]=dp[i-][j-]*j+dp[i-][j]*j;
for(i=;i<=cnt;i++)ans+=dp[cnt][i];
return ans;
}
};

PossibleOrders TopCoder - 1643的更多相关文章

  1. 【Topcoder 1643】PossibleOrders

    题意:给一些等价关系,问把所有的数按照大小排序的种类数. 思路:首先并查集维护等价类,然后设有\(n\)个等价类. 那么就可以\(dp\)了. 考虑\(dp(i)\)表示还剩下\(i\)个等价类,答案 ...

  2. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  3. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  4. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  5. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  6. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  7. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  8. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  9. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

随机推荐

  1. yeomen/bower/grunt

    yeomen: npm install yo angular-in-action project npm install -g generator-angular npm install -g gen ...

  2. uva11292 Dragon of Loowater(排序后贪心)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  3. AJAX的最小单元

    $(function(){ $('#send').click(function(){ $.ajax({ type: "GET", url: "test.json" ...

  4. LeetCode Perfect Number

    原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...

  5. 用javascript实现base64编码器以及图片的base64编码

    前面的话 base-64作为常见的编码函数,在基本认证.摘要认证以及一些HTTP扩展中得到了大量应用.在前端领域,也常常把图片转换为base-64编码在网络中传输.本文将详细介绍base64的原理及用 ...

  6. poj 2096 , zoj 3329 , hdu 4035 —— 期望DP

    题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...

  7. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  8. file“xxxxx”has modification times xxxxx s in the future..

    这是因为一个项目从一个电脑拷贝的到另一个电脑上时,两个电脑的时钟不一致所致,修改一下项目所在目录的修改时间即可: find /your/dir -type f -exec touch {} + 也可以 ...

  9. Angular5学习笔记 - 配置Http(七)

    一.引入Http模块 编辑\src\app\app.module.ts文件 import { HttpModule } from '@angular/http'; /* 注册模块 */ imports ...

  10. 使用Spring Boot 和Spring Data JPA访问mysql数据库

    在Spring中使用JdbcTemplate是一种基本的数据访问方式,但是仍然需要较多的代码,为了解决这些大量枯燥的数据操作语句,我们可以使用ORM框架,比如:Hibernate,通过整合Hibern ...