怎樣學習哲學
阿新 • • 發佈:2017-09-19
+= som bsp 此外 sizeof 技術 times memset char
長者回答:“你啊,Too Young Too Simple,Sometimes Naive!哲學這種東西,不是說想懂就能懂的,需要靜心撕烤。你去後面的森林裏好好想想。” 長者的後院有一片哲♂學森林。由於一些奧妙重重的原因,這片森林構成了一個n*m的矩形,其中每個點就代表了一棵樹。此外,由於辣雞出題人KJDH從中搗鬼,有些樹被連根拔起(也就是消失了)。抖兒每天都要到樹下撕烤,因此他想要在每一行選擇一棵樹。但是他非常討厭走回頭路,因此第i行選擇的樹必須比第i-1行的靠右。現在抖兒想知道,總共有多少種選擇的方案。
第一行三個整數n,m,p,分別表示森林的長、寬,以及消失的樹的數目。
接下來p行每行兩個整數,表示第ai行第bi列的樹消失了。
方案一:選(1,1)(2,2)(3,3)
方案二:選(1,1)(2,2)(3,5)
方案三:選(1,1)(2,4)(3,5)
方案四:選(1,2)(2,4)(3,5)
方案五:選(1,3)(2,4)(3,5)
【限制與約定】
對於所有的數據,保證n,m≤109,p≤min(n*m,2000)
【後記】
在經歷了長久的撕烤之後,抖兒終於領悟了哲♂學奧義。
抖兒對長者說:“我知道了!哲學源於生活,只有撕烤生活,才能領悟哲理。”
長者嘿嘿一笑:“你想多了。因為你在哲♂學之森中待的時間太長,政治學考已經在一個月前結束了。” 題解:話說我也是逃了政治課上來的。 這題50分直接動規。 先考慮p=0的情況,C(n,m)對吧。 那p!=0時我們只要減去不行的就可以了。 不行的怎麽求不會重復那?我們只要在一條不合法的方案在第一個不合法的點減掉就可以了。 令dp[i]表示到達i且僅經過i這個空點方案總數。 那麽dp[i]=起始點到i的方案數-dp[j]*j到i的方案數。其中j是所有在i左上方的空點。 代碼:
怎樣學習哲學
時間限制: 1 Sec 內存限制: 128 MB題目描述
OI大師抖兒在奪得銀牌之後,順利保送pku。這一天,抖兒問長者:“雖然我已經保送了,但是我還要參加學考。馬上就要考政治了,請問應該怎樣學習哲學,通過政治考試?”長者回答:“你啊,Too Young Too Simple,Sometimes Naive!哲學這種東西,不是說想懂就能懂的,需要靜心撕烤。你去後面的森林裏好好想想。” 長者的後院有一片哲♂學森林。由於一些奧妙重重的原因,這片森林構成了一個n*m的矩形,其中每個點就代表了一棵樹。此外,由於辣雞出題人KJDH從中搗鬼,有些樹被連根拔起(也就是消失了)。抖兒每天都要到樹下撕烤,因此他想要在每一行選擇一棵樹。但是他非常討厭走回頭路,因此第i行選擇的樹必須比第i-1行的靠右。現在抖兒想知道,總共有多少種選擇的方案。
輸入
輸出
一行一個整數,表示方案數。由於答案可能很大,請對1000003取模。樣例輸入
3 5 2 2 3 3 4樣例輸出
5提示
【樣例說明】
方案一:選(1,1)(2,2)(3,3)
方案二:選(1,1)(2,2)(3,5)
方案三:選(1,1)(2,4)(3,5)
方案四:選(1,2)(2,4)(3,5)
方案五:選(1,3)(2,4)(3,5)
【限制與約定】
測試點編號 |
n,m |
p |
1 | n,m≤10 |
|
2 |
||
3 |
||
4 |
||
5 |
n,m≤100 |
|
6 |
||
7 |
||
8 |
||
9 |
n,m≤5000 |
|
10 |
||
11 |
n,m≤100000 n,m≤100000 |
p=0 |
12 |
||
13 |
||
14 |
||
15 |
n,m≤109 |
p=0 |
16 |
||
17 |
||
18 |
||
19 |
||
20 |
對於所有的數據,保證n,m≤109,p≤min(n*m,2000)
【後記】
在經歷了長久的撕烤之後,抖兒終於領悟了哲♂學奧義。
抖兒對長者說:“我知道了!哲學源於生活,只有撕烤生活,才能領悟哲理。”
長者嘿嘿一笑:“你想多了。因為你在哲♂學之森中待的時間太長,政治學考已經在一個月前結束了。” 題解:話說我也是逃了政治課上來的。 這題50分直接動規。 先考慮p=0的情況,C(n,m)對吧。 那p!=0時我們只要減去不行的就可以了。 不行的怎麽求不會重復那?我們只要在一條不合法的方案在第一個不合法的點減掉就可以了。 令dp[i]表示到達i且僅經過i這個空點方案總數。 那麽dp[i]=起始點到i的方案數-dp[j]*j到i的方案數。其中j是所有在i左上方的空點。 代碼:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld; 11 typedef pair<int,int> pr; 12 const double pi=acos(-1); 13 #define rep(i,a,n) for(int i=a;i<=n;i++) 14 #define per(i,n,a) for(int i=n;i>=a;i--) 15 #define Rep(i,u) for(int i=head[u];i;i=Next[i]) 16 #define clr(a) memset(a,0,sizeof(a)) 17 #define pb push_back 18 #define mp make_pair 19 #define fi first 20 #define sc second 21 #define pq priority_queue 22 #define pqb priority_queue <int, vector<int>, less<int> > 23 #define pqs priority_queue <int, vector<int>, greater<int> > 24 #define vec vector 25 ld eps=1e-9; 26 ll pp=1000000007; 27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;} 28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;} 29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } 30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; } 31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1}; 32 ll read(){ ll ans=0; char last=‘ ‘,ch=getchar(); 33 while(ch<‘0‘ || ch>‘9‘)last=ch,ch=getchar(); 34 while(ch>=‘0‘ && ch<=‘9‘)ans=ans*10+ch-‘0‘,ch=getchar(); 35 if(last==‘-‘)ans=-ans; return ans; 36 } 37 const int p=1000003; 38 struct node{ 39 int a,b; 40 }f[3000]; 41 bool cmp(node a,node b){ 42 return (a.a<b.a)||(a.a==b.a && a.b<b.b); 43 } 44 ll dp[3000],fac[p],inv[p]; 45 ll C(ll x,ll y){ 46 if (y>x) return 0; 47 if (x<p && y<p) return fac[x]*inv[y]%p*inv[x-y]%p; 48 return C(x/p,y/p)*C(x%p,y%p)%p; 49 } 50 int main(){ 51 int n=read(),m=read(),k=read(); 52 for (int i=1;i<=k;i++) f[i].a=read(),f[i].b=read(); 53 int i; 54 for (fac[0]=1,i=1;i<p;i++) fac[i]=fac[i-1]*i%p; 55 for (inv[1]=1,i=2;i<p;i++) inv[i]=(p-p/i)*inv[p%i]%p; 56 for (inv[0]=1,i=1;i<p;i++) inv[i]=inv[i-1]*inv[i]%p; 57 ll sum=C(m,n); 58 k++; f[k].a=n+1; f[k].b=m+1; 59 sort(f+1,f+k+1,cmp); 60 for (int i=1;i<=k;i++){ 61 dp[i]=C(f[i].b-1,f[i].a-1); 62 for (int j=1;j<i;j++) 63 if (f[i].b>f[j].b && f[i].a>f[j].a) 64 dp[i]=((dp[i]-dp[j]*C(f[i].b-f[j].b-1,f[i].a-f[j].a-1)%p)%p+p)%p; 65 sum=((sum-dp[i]*C(m-f[i].b,n-f[i].a)%p)%p+p)%p; 66 } 67 printf("%lld",sum); 68 return 0; 69 }View Code
怎樣學習哲學