AtCoder ABC 127E Cell Distance

Topic links: https://atcoder.jp/contests/abc127/tasks/abc127_e

Subject to the effect

  Given a $ N * M $ board, tuple $ (x, y), 1 \ leq x \ leq N, 1 \ leq y \ leq M $, represents a certain position on the board, the board now K is selected from the different positions, referred to as $ (x_1, y_1), (x_2, y_2), \ dots, (x_K, y_K) corresponding cost of $, selected for $ \ sum_ {i = 1} ^ {K- 1} \ sum_ {j = i + 1} ^ K $ sum of the cost, all possible output schemes (| x_i - | - x_j | | + y_j y_i).

analysis

  First difficult to find, x and y can be calculated separately, the only requirement $ \ sum_ {i = 1} ^ {K-1} \ sum_ {j = i + 1} ^ K | x_i - x_j | $ i.e. can, similarly calculated $ \ sum_ {i = 1} ^ {K-1} \ sum_ {j = i + 1} ^ K | y_i - y_j | $.
  If we fix the position of the board 2 $ x_ {i_1, j_1}, x_ {i_2, j_2} $ does not move, in this case, $ \ tbinom {N * M-2} {K-2} $ species option, in other words $ | x_ {i_1, j_1} - x_ {i_2, j_2} | $ appeared $ \ tbinom {N * M-2} {K-2} $ times.
  But to enumerate all the points undoubtedly to overtime, for which we can enumerate $ d = | x_ {i_1, j_1} - x_ {i_2, j_2} | $, $ d \ in [1, K - 1] $ .
  Law can first look, when d == 1, the number of look $ (x_ {i_1, j_1}, x_ {i_2, j_2}) $ is satisfied.
  It can be found as long as the difference between the two points is a vertical axis, they all satisfied.
  So when d == 1 when there is $ M ^ 2 * (N - 1) $ species $ (x_ {i_1, j_1}, x_ {i_2, j_2}) $ satisfies $ d == | x_ {i_1, j_1} - x_ {i_2, j_2} | $.
  And so on, can find out the rules: for each d, has $ d * M ^ 2 * (N - d) $ species $ (x_ {i_1, j_1}, x_ {i_2, j_2}) $ $ d satisfies == | x_ {i_1, j_1} - x_ {i_2, j_2} | $.
  Then for each d, its contribution to the answer is $ \ tbinom {N * M-2} {K-2} * d * M ^ 2 * (N - d) $.
  All add up to.

code show as below

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3  
  4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  5 #define Rep(i,n) for (int i = 0; i < (n); ++i)
  6 #define For(i,s,t) for (int i = (s); i <= (t); ++i)
  7 #define rFor(i,t,s) for (int i = (t); i >= (s); --i)
  8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
  9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
 10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
 12  
 13 #define pr(x) cout << #x << " = " << x << "  "
 14 #define prln(x) cout << #x << " = " << x << endl
 15  
 16 #define LOWBIT(x) ((x)&(-x))
 17  
 18 #define ALL(x) x.begin(),x.end()
 19 #define INS(x) inserter(x,x.begin())
 20  
 21 #define ms0(a) memset(a,0,sizeof(a))
 22 #define msI(a) memset(a,inf,sizeof(a))
 23 #define msM(a) memset(a,-1,sizeof(a))
 24 
 25 #define MP make_pair
 26 #define PB push_back
 27 #define ft first
 28 #define sd second
 29  
 30 template<typename T1, typename T2>
 31 istream &operator>>(istream &in, pair<T1, T2> &p) {
 32     in >> p.first >> p.second;
 33     return in;
 34 }
 35  
 36 template<typename T>
 37 istream &operator>>(istream &in, vector<T> &v) {
 38     for (auto &x: v)
 39         in >> x;
 40     return in;
 41 }
 42  
 43 template<typename T1, typename T2>
 44 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
 45     out << "[" << p.first << ", " << p.second << "]" << "\n";
 46     return out;
 47 }
 48 
 49 inline int gc(){
 50     static const int BUF = 1e7;
 51     static char buf[BUF], *bg = buf + BUF, *ed = bg;
 52     
 53     if(bg == ed) fread(bg = buf, 1, BUF, stdin);
 54     return *bg++;
 55 } 
 56 
 57 inline int ri(){
 58     int x = 0, f = 1, c = gc();
 59     for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
 60     for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
 61     return x*f;
 62 }
 63  
 64 typedef long long LL;
 65 typedef unsigned long long uLL;
 66 typedef pair< double, double > PDD;
 67typedef pair < int , int > PII;
68 typedef pair < string , int > PSI;
69 typedef Set < int > SI;
70 typedef vector < int > VI;
71 typedef vector <PII> VPII;
72 typedef map < int , int > MII;
73 typedef pair <LL, LL> PLL;
74 typedef vector <LL> VL;
75 typedef vector <VL> VVL;
 76 const double EPS = 1e-10;
 77 const LL inf = 0x7fffffff;
 78 const LL infLL = 0x7fffffffffffffffLL;
 79 const LL mod = 1e9 + 7;
 80 const int maxN = 2e5 + 7;
 81 const LL ONE = 1;
 82 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
 83 const LL oddBits = 0x5555555555555555;
 84 
 85 LL fac[maxN];
86  void init_fact () {
 87      FAC [ 0 ] = . 1 ;
 88      the For (I, . 1 , maxN - . 1 ) {
 89          FAC [I] = (I * FAC [I - . 1 ])% MOD;
 90      }
 91 is  }
 92  
93  // AX + by = GCD (A, B) = D
 94  // extended Euclidean algorithm 
95  / * *
 96  * A + B * X * Y. 1 =
 97  * ab If prime, solvability
 98  * x b is the inverse element on a
 99  * Y b is the inverse of the element on a
 100  *     
101  * proof: 
 102  * A * X% B + B * Y% B =. 1% B
 103  * A * X% B =. 1% B
 104  * A * X =. 1 (MOD B)
 105   * / 
106 inline void ex_gcd (LL A, LL B, LL & X, LL & Y, LL & D) {
 107      IF (B!) {D = A, X = . 1 , Y = 0 ;}
 108      the else {
 109          ex_gcd (B, A% B, Y, X, D);
 110          Y - X * = (a / B);
 111      }
 112  }
 113  
114  // find the inverse element on a p, and if there is, return -1 
 115 // a与p互质,逆元才存在 
116 inline LL inv_mod(LL a, LL p = mod){
117     LL d, x, y;
118     ex_gcd(a, p, x, y, d);
119     return d == 1 ? (x % p + p) % p : -1;
120 }
121 
122 inline LL comb_mod(LL m, LL n) {
123     LL ret;
124     if(m > n) swap(m, n);
125     ret = (fac[n] * inv_mod(fac[m], mod)) % mod;
126     ret = (ret * inv_mod(fac[n - m], mod)) % mod;
127     return ret;
128 }
129 
130 void add_mod(LL &a, LL b) {
131     a = (a + b) % mod;
132     if(a < 0) a += mod;
133 }
134 
135 int N, M, K;
136 LL ans;
137 
138 int main(){
139     INIT(); 
140     init_fact();
141     cin >> N >> M >> K;
142     LL cnt = comb_mod(K - 2, N * M - 2);
143     
144     ForLL(d, 1, N - 1) add_mod(ans, cnt * ((d * M * M * (N - d)) % mod));
145     ForLL(d, 1, M - 1) add_mod(ans, cnt * ((d * N * N * (M - d)) % mod));
146     cout << ans << endl;
147     return 0;
148 }
View Code

 

Guess you like

Origin www.cnblogs.com/zaq19970105/p/10941594.html