UVA11538




Hint :


※ 假設M邊較長      


總共有三種方法可以攻擊敵人,行、列、斜向。

行與列:

黑后有M*N種位置選擇,選擇M以後白后就只剩下N-1個位置,得 M*N(N-1),etc

得 N*M(M-1),整合為 N*M( N + M - 2) 。


斜向:最長的斜向一共有 M - N + 1條,假定最長斜向為 n length

可得1,2,3......n-1,nnnnnn(M-N+1條) ,n-1,n-2,.......1,令 1 ~ n-1 加總為 i

故可得一共有 2 * ∑i ( i-1 ) (1<= i <=n-1) 條斜向(不含最長)

最長的斜向:N ( N-1 ) ( M - N + 1) ,選了一個N即剩下N-1種選擇

斜向總數:

( 2 * ∑i ( i - 1 ) (1<= i <=n-1) + N( N - 1 ) ( M - N + 1) )  * 2 (正反斜)

↑↓ (整理結果)

2 * N * ( N-1 ) * (3 * M - N - 1) / 3        





import java.util.Scanner;

public class UVA11538 {

  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc = new Scanner(System.in);

  while(sc.hasNext())
  {
   long N = 0L,M = 0L;

    N = sc.nextInt();

    M = sc.nextInt();

    if(N==0&&M==0)break;

   long t=0L;

   if(N>M)
   {
    t=M;
    M=N;
    N=t;
   }

   long diagonal = 0L,ans = 0L;

   ans += N * M * ( N + M -2);

   diagonal = 2 * N * (N-1) *(3*M - N -1) / 3 ;

   ans+=diagonal;

   System.out.println(ans);
  }

  sc.close();
 }

}

/*A free-standing sequence of digits is interpreted by the compiler as an int literal
 *
 *so, you need to use:    long xxx = xxx L , to prevent the overflow  
*/