To my boyfriend
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1246 Accepted Submission(s): 556
I never forget the moment I met with you. You carefully asked me: "I have a very difficult problem. Can you teach me?". I replied with a smile, "of course". You replied:"Given a matrix, I randomly choose a sub-matrix, what is the expectation of the number of **different numbers** it contains?"
Sincerely yours,
Guo
Each case contains two integers, n and m (1≤n, m≤100), the number of rows and the number of columns in the grid, respectively.
The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains g_i,j (0≤ g_i,j < n*m).
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define mp make_pair #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 1e2+50;; const int M = 255; const int mod = 19260817; const int mo=123; const double pi= acos(-1.0); typedef pair<int,int>pii; int n,m; int a[N][N]; int l[N],r[N],pos[N][N*N]; ll ans; void solve(int x,int y,int col){ int ly=0,ry=m+1; for(int i=1;i<=n;i++)l[i]=0,r[i]=m+1; for(int i=1;i<y;i++)l[pos[i][col]]=i; for(int i=m;i>y;i--)r[pos[i][col]]=i; for(int i=x;i>pos[y][col];i--){ ly=max(ly,l[i]); ry=min(ry,r[i]); ans+=(ll)(n-x+1)*(ry-y)*(y-ly); } } int main(){ int T; scanf("%d",&T); while(T--){ met(pos,0);ans=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); solve(i,j,a[i][j]); pos[j][a[i][j]]=i; } } ll all=(ll)n*(n+1)*m*(m+1)/4; printf("%.9f\n",(double)ans/all); } return 0; }