Advertisement
Guest User

Untitled

a guest
May 25th, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #pragma GCC optimize("O3")
  2. #pragma GCC target("sse4")
  3. // #pragma GCC optimize("Ofast")  
  4. // #pragma GCC target("avx,avx2,fma")
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7. typedef long long ll;
  8. const double PI=acos(-1.0);
  9. #define t1(x)             cerr<<#x<<"="<<x<<endl
  10. #define t2(x, y)          cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
  11. #define t3(x, y, z)       cerr<<#x<<"=" <<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
  12. #define t4(a,b,c,d)       cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<endl
  13. #define t5(a,b,c,d,e)     cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<endl
  14. #define t6(a,b,c,d,e,f)   cerr<<#a<<"="<<a<<" "<<#b<<"="<<b<<" "<<#c<<"="<<c<<" "<<#d<<"="<<d<<" "<<#e<<"="<<e<<" "<<#f<<"="<<f<<endl
  15. #define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
  16. #define tr(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
  17. #define __ freopen("tictactoe.in","r",stdin);freopen("tictactoe.out","w",stdout);
  18. #define fastio() ios::sync_with_stdio(0);cin.tie(0)
  19. #define MEMS(x,t) memset(x,t,sizeof(x));
  20. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  21. /*-------------------------------------------------------------------------------------------------------------------------------------*/
  22. #define MOD 1000000007
  23. #define endl "\n"
  24. #define int long long
  25. #define inf 1e18
  26. #define ld long double
  27. /*-------------------------------------------------------------------------------------------------------------------------------------*/
  28.  
  29. ll poww(long long a, long long n, long long m)
  30. {
  31.         long long ans = 1;
  32.         long long mul = a;
  33.         while (n != 0)
  34.         {
  35.                 if (n % 2)
  36.                 {
  37.                         ans = (ans * mul) % m;
  38.                 }
  39.                 mul = (mul * mul) % m;
  40.                 n /= 2;
  41.         }
  42.         return ans;
  43. }
  44.  
  45. const int SZ=2e6+100;
  46. ll factorial[SZ];
  47. ll iv[SZ];
  48. void precompute()
  49. {
  50.     factorial[0]=1;
  51.     iv[0]=1;
  52.     for(int i=1;i<SZ;i++)
  53.     {
  54.         factorial[i]=(factorial[i-1]*i)%MOD;
  55.         iv[i]=poww(factorial[i],MOD-2,MOD);
  56.     }
  57. }
  58. ll InverseEuler(ll n, ll m)
  59. {
  60.     return poww(n, m - 2, m);
  61. }
  62. ll C(ll n, ll r, ll m)
  63. {
  64.     if(r>n || r<0)return 0;
  65.     return (factorial[n] * ((iv[r] * iv[n-r]) % m)) % m;
  66. }
  67.  
  68.  
  69. const int N=1e3+10;
  70. int dis[N][N];
  71.  
  72.    
  73. int dx[]={-2,-2,-1,-1,+1,+1,2,2};
  74. int dy[]={-1,1,-2,2,-2,2,-1,1};
  75.  
  76. int n,m,k;
  77. vector<int> d;
  78. bool check(int x,int y)
  79. {
  80.     return x>=0 && y>=0 && x<n && y<m;
  81. }
  82. int get_frac(int a,int b)
  83. {
  84.     a%=MOD;
  85.     return (a*poww(b,MOD-2,MOD))%MOD;
  86. }
  87. // int fact[N*N];
  88. signed main()
  89. {
  90.     fastio();
  91.     // fact[0]=1;
  92.     // for(int i=1;i<N*N;i++)
  93.     // {
  94.         // fact[i]=fact[i-1]*i;
  95.         // fact[i]%=MOD;
  96.     // }
  97.     precompute();
  98.     int t;
  99.     cin>>t;    
  100.     while(t--)
  101.     {
  102.         cin>>n>>m>>k;
  103.         string s[n];
  104.         for(int i=0;i<n;i++)
  105.         {
  106.             cin>>s[i];
  107.         }
  108.         for(int i=0;i<n;i++)
  109.         {
  110.             for(int j=0;j<m;j++)
  111.             {
  112.                 dis[i][j]=inf;
  113.             }
  114.         }
  115.         queue<pair<pair<int,int>,int>> q;
  116.         for(int i=0;i<n;i++)
  117.         {
  118.             for(int j=0;j<m;j++)
  119.             {
  120.                 if(s[i][j]=='X')
  121.                 {
  122.                     q.push({{i,j},0});
  123.                     dis[i][j]=0;
  124.                 }
  125.             }
  126.         }
  127.         while(q.size())
  128.         {
  129.             auto tmp=q.front();
  130.             q.pop();
  131.             int x=tmp.first.first;
  132.             int y=tmp.first.second;
  133.             int d=tmp.second;
  134.             dis[x][y]=d;
  135.             for(int j=0;j<8;j++)
  136.             {
  137.                 for(int kj=0;kj<8;kj++)
  138.                 {
  139.                     if(check(x+dx[j],y+dy[kj]) && dis[x+dx[j]][y+dy[kj]]>d+1)
  140.                     {
  141.                         dis[x+dx[j]][y+dy[kj]]=d+1;
  142.                         q.push({{x+dx[j],y+dy[kj]},d+1});
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         d=vector<int>(n*m+1,0);
  148.        
  149.         for(int i=0;i<n;i++){
  150.             for(int j=0;j<m;j++){
  151.                 assert(dis[i][j]<inf);             
  152.                 d[dis[i][j]]++;
  153.             }
  154.         }
  155.         int ans=0;
  156.         int total=0;
  157.         vector<int> p_atmost(n*m+1,0);
  158.         vector<int> p2(n*m+1,0);
  159.         for(int i=0;i<=n*m;i++)
  160.         {
  161.             total+=d[i];
  162.            
  163.             //prob of max distance atmost i
  164.             p_atmost[i]=(C(total,k,MOD));
  165.            
  166.             p2[i]=p_atmost[i];
  167.             if(i>0)
  168.                 p2[i]=p_atmost[i]-p_atmost[i-1];
  169.             if(p2[i]<0)p2[i]+=MOD;
  170.            
  171.             ans+=(p2[i]*i)%MOD;
  172.             ans%=MOD;
  173.         }      
  174.         ans*=poww(C(n*m,k,MOD),MOD-2,MOD);ans%=MOD;
  175.         cout<<ans<<endl;
  176.     }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement