創作內容

2 GP

UVa 1264題解

作者:音楓語然│2018-03-09 22:28:38│巴幣:4│人氣:139
如題這是UVa 1264的題解
我被坑到MOD那邊,很低能,不要學我

首先用C[i][j]代表C[i][j],千萬不要用F[i]當作i!然後手作組合數,這樣子在模的時候會爆

絕對不能那樣做

然後題目看了一下,其實不難,意思就是當左序列長度為A有n種可行排列,右序列長度為B有m種可行排列

此時把他們併成A+B長度的序列的情況下然後各別序列相對位置不變的排法有幾種

例如A是1 2 3
B是4 5 6
那麼
123456
142536
124536
都是可以的
但125436就是不行的

然後怎麼算呢,左邊有n種排法右邊有m種排法

那麼先從n+m中選出n 個位置再乘以n跟m就好

程式碼如下

#include<cstdio>
#include<iostream>
#define MOD 9999991
#define endl '\n'
#define int long long int
using namespace std;
int C[30][30];
void init(){
for(int i=0;i<30;i++){
C[i][0]=C[i][i]=1;
for(int j=1;j<i;j++)
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
struct BTS{
struct Node{
int k,si;
Node*ch[2];
Node(){}
Node(int _x):k(_x){ch[0]=ch[1]=NULL;si=1;}
}*root;
int si;
void init(int n){
this->si=n;
root=NULL;
}
void ins(Node*&cur,int x){
if(cur==NULL){
cur=new Node(x);
return;
}
if(x<cur->k)ins(cur->ch[0],x);
else ins(cur->ch[1],x);
cur->si=1;
if(cur->ch[0])cur->si+=cur->ch[0]->si;
if(cur->ch[1])cur->si+=cur->ch[1]->si;
}
int count(Node*cur){
if(cur==NULL)return 1;
if(cur->ch[0]&&!cur->ch[1]){
return count(cur->ch[0])%MOD;
}
else if(cur->ch[0]&&cur->ch[1]){
int sp=cur->ch[0]->si+cur->ch[1]->si;
return count(cur->ch[0])%MOD*count(cur->ch[1])%MOD*(C[sp][cur->ch[0]->si])%MOD;
}
else if(!cur->ch[0]&&cur->ch[1]){
return count(cur->ch[1])%MOD;
}
else {
return 1;
}
}
}tree;
main(){
cin.tie(0);ios_base::sync_with_stdio(false);
init();
int T;cin>>T;
while(T--){
int N;cin>>N;
tree.init(N);
for(int i=0;i<N;i++){
int a;cin>>a;
tree.ins(tree.root,a);
}
cout<<tree.count(tree.root)<<endl;
}
return 0;
}

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3914695
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 1 篇留言

0.0
好電喔,你以為有人在乎UVa嗎?

08-25 18:27

我要留言提醒:您尚未登入,請先登入再留言

2喜歡★choder123456 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:我要來分享我對上古卷軸的... 後一篇:教你如何消掉arch l...

追蹤私訊切換新版閱覽

作品資料夾

hyzgdivina喜歡虹咲的LLer
我的小屋裡有很多又香又甜的Hoenn繪師虹咲漫畫翻譯喔!歡迎LoveLiver來我的小屋裡坐坐~看更多我要大聲說13小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】