鴿巢原理(抽屜原理)
鴿巢原理(抽屜原理)
基本描述
桌子上有是個蘋果,把這十個蘋果放到九個抽屜裡,無論怎麼放,我們會發現至少會有一個抽屜裡面至少放兩個蘋果。這一現象就是所說的“抽屜原理”。
更一般的表述:如果每一個抽屜代表一個集合,每一個蘋果就可以代表一個元素。加入有n+1個元素放到n個集合中去,其中必定有一個集合裡至少有兩個元素。
第一抽屜原理
原理1
把多餘n+1個物體放到n個抽屜裡,則至少有一個抽屜裡的東西不少於兩件。
原理2
把多餘mn+1(n不為0)個物體放到n個抽屜裡面,則至少有一個抽屜裡面不少於(m+1)的物體。
第二抽屜原理
把(mn -1 )個物體放入n個抽屜中,其中必須有一個抽屜不多餘(m-1)個物體。
舉例
屬相問題
屬相有12個,那麼任意37個人中,至少有幾個人屬相相同?
上取整(37 / 12) = 4
招聘問題
有300人到招聘會求職,其中軟體設計有100人,市場營銷有80人,財務管理有70人,人力資源管理有50人。那麼至少有多少人找到工作才能保證一定有70人找的工作專業相同?
考慮最差情況,即軟體設計,市場營銷,財務管理均招了69人,人力資源管理招了50人,此時再多招1人,就有70人找的工作專業相同了。
故答案為 69*3 + 50 + 1 = 258
襯衫問題
一個抽屜裡有20件襯衫,其中4件是藍的,7件是灰的,9件是紅的,則應從中隨意取出多少件才能保證有5件是同顏色的?
考慮最差情況,即已經取出了4件藍色,4件灰色,4件紅色,再多取出1件就滿足條件。
故答案為 4 + 4 + 4 + 1 = 13。
例題:
1.poj 2356:https://vjudge.net/problem/POJ-2356
題意:給你n個大於0的數,在n中取k個數且其和為n的倍數,輸出k和每個數。
思路:我們可以從字首和考慮,如果sum[i]%n那麼直接輸出,否則考慮sum[i]%n因為這n個數一定是屬於[1,n-1]這就相當於把n個物品放到n-1個抽屜裡,那麼一定存在sum[i]%n=sum[j]%n,那麼(sum[j]-sum[i])%n==0,假設j>i,那麼我們只要輸出ai-aj就好了。
1 #include<bits/stdc++.h> 2 #include<time.h> 3 #include <set> 4 #include <map> 5 #include <stack> 6 #include <cmath> 7 #include <queue> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstring> 12 #include <utility> 13 #include <cstring> 14 #include <iostream> 15 #include <algorithm> 16 #include <list> 17 using namespace std; 18 #define eps 1e-10 19 #define PI acos(-1.0) 20 #define lowbit(x) ((x)&(-x)) 21 #define zero(x) (((x)>0?(x):-(x))<eps) 22 #define mem(s,n) memset(s,n,sizeof s); 23 #define ios {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);} 24 typedef long long ll; 25 typedef unsigned long long ull; 26 const int maxn=1e5+5; 27 const int Inf=0x7f7f7f7f; 28 const ll Mod=999911659; 29 //const int N=3e3+5; 30 bool isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; }//判斷一個數是不是 2 的正整數次冪 31 int modPowerOfTwo(int x, int mod) { return x & (mod - 1); }//對 2 的非負整數次冪取模 32 int getBit(int a, int b) { return (a >> b) & 1; }// 獲取 a 的第 b 位,最低位編號為 0 33 int Max(int a, int b) { return b & ((a - b) >> 31) | a & (~(a - b) >> 31); }// 如果 a>=b,(a-b)>>31 為 0,否則為 -1 34 int Min(int a, int b) { return a & ((a - b) >> 31) | b & (~(a - b) >> 31); } 35 ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} 36 ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} 37 int Abs(int n) { 38 return (n ^ (n >> 31)) - (n >> 31); 39 /* n>>31 取得 n 的符號,若 n 為正數,n>>31 等於 0,若 n 為負數,n>>31 等於 -1 40 若 n 為正數 n^0=n, 數不變,若 n 為負數有 n^(-1) 41 需要計算 n 和 -1 的補碼,然後進行異或運算, 42 結果 n 變號並且為 n 的絕對值減 1,再減去 -1 就是絕對值 */ 43 } 44 ll binpow(ll a, ll b,ll c) { 45 ll res = 1; 46 while (b > 0) { 47 if (b & 1) res = res * a%c; 48 a = a * a%c; 49 b >>= 1; 50 } 51 return res%c; 52 } 53 void extend_gcd(ll a,ll b,ll &x,ll &y) 54 { 55 if(b==0) { 56 x=1,y=0; 57 return; 58 } 59 extend_gcd(b,a%b,x,y); 60 ll tmp=x; 61 x=y; 62 y=tmp-(a/b)*y; 63 } 64 ll mod_inverse(ll a,ll m) 65 { 66 ll x,y; 67 extend_gcd(a,m,x,y); 68 return (m+x%m)%m; 69 } 70 ll eulor(ll x) 71 { 72 ll cnt=x; 73 ll ma=sqrt(x); 74 for(int i=2;i<=ma;i++) 75 { 76 if(x%i==0) cnt=cnt/i*(i-1); 77 while(x%i==0) x/=i; 78 } 79 if(x>1) cnt=cnt/x*(x-1); 80 return cnt; 81 } 82 ll n,a[maxn],sum[maxn],pos[maxn]; 83 int main() 84 { 85 ios 86 cin>>n; 87 for(int i=1;i<=n;i++) 88 { 89 cin>>a[i]; 90 sum[i]=sum[i-1]+a[i]; 91 } 92 for(int i=1;i<=n;i++) 93 { 94 if(sum[i]%n==0) 95 { 96 cout<<i<<endl; 97 for(int j=1;j<=i;j++) cout<<a[j]<<endl; 98 break; 99 } 100 if(pos[sum[i]%n]) 101 { 102 cout<<i-pos[sum[i]%n]<<endl; 103 for(int j=pos[sum[i]%n]+1;j<=i;j++) cout<<a[j]<<endl; 104 break; 105 } 106 pos[sum[i]%n]=i; 107 } 108 return 0; 109 }View Code