計算 A×B 矩形網格中矩形總數。同一直線上 n 個點可構成 (n-1)n/2 條線段,故矩形總數 = (A+1)A/2 × (B+1)B/2。當 A,B 取 1000 時結果超過 32/64 位整數範圍,需高精度運算。
以基數 10⁶(POW=1000000)陣列儲存大整數,每元素存 6 位十進制數字。實現乘法 mul、加法進位、輸出格式化(%06d)。
mul_sum(BIG_NUM m, int n):計算 n(n+1)/2 並存入高精度陣列,處理奇偶分流避免溢位。mul(BIG_NUM m, int n):高精度乘以單精度整數。count(BIG_NUM m, int A, int B):調用上述函數得出最終結果。#include <stdio.h>
#define POW 1000000
#define WID 6
typedef int BIG_NUM[100];
void mul(BIG_NUM m, int n) { ... }
void mul_sum(BIG_NUM m, int n) { ... }
void count(BIG_NUM m, int A, int B) { ... }
int main() {
BIG_NUM m;
int A, B;
while (scanf("%d %d", &A, &B) == 2) {
count(m, A, B);
// 輸出高精度結果
}
return 0;
}