Problem p02861 (C++)

Buggy submission (status: Runtime Error):
// #include "stdafx.h"
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF 2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define BitSetV(Val, Bit) ((Val) |= (Bit))
#define BitTstV(Val, Bit) ((Val) & (Bit))
#define ArrayLength(x) (sizeof(x) / sizeof(x[0]))
#define MAX_DWORD (0xFFFFFFFF)
#define MAX_SDWORD ((SDWORD)0x7FFFFFFF)
#define MIN_SDWORD ((SDWORD)0x80000000)
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define MIN_QWORD ((QWORD)0x0000000000000000)
#define MAX_SQWORD ((SQWORD)0x7FFFFFFFFFFFFFFF)
#define MIN_SQWORD ((SQWORD)0x8000000000000000)
#define M_PI 3.14159265358979323846

typedef long SDWORD;
typedef long long SQWORD;
typedef unsigned long DWORD;
typedef unsigned long long int QWORD;
typedef long long ll;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
int main() {
  int N;
  cin >> N;
  int x[8], y[8];
  REP(i, N) cin >> x[i] >> y[i];

  // idx
  vector<int> idx(N);
  REP(i, 8) idx[i] = i;

  // 順列
  double ans = 0;
  do {
    for (int i = 0; i < N - 1; i++) {
      double diffx = x[idx[i]] - x[idx[i + 1]];
      double diffy = y[idx[i]] - y[idx[i + 1]];
      ans += sqrt((pow(diffx, 2) + pow(diffy, 2)));
    }
  } while (next_permutation(idx.begin(), idx.end()));

  // 個数
  double div = 1;
  for (int i = 1; i <= N; i++) {
    div *= i;
  }
  ans = ans / div;
  printf("%lf\n", ans);
  return 0;
}


Fixed submission:
// #include "stdafx.h"
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF 2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define BitSetV(Val, Bit) ((Val) |= (Bit))
#define BitTstV(Val, Bit) ((Val) & (Bit))
#define ArrayLength(x) (sizeof(x) / sizeof(x[0]))
#define MAX_DWORD (0xFFFFFFFF)
#define MAX_SDWORD ((SDWORD)0x7FFFFFFF)
#define MIN_SDWORD ((SDWORD)0x80000000)
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define MIN_QWORD ((QWORD)0x0000000000000000)
#define MAX_SQWORD ((SQWORD)0x7FFFFFFFFFFFFFFF)
#define MIN_SQWORD ((SQWORD)0x8000000000000000)
#define M_PI 3.14159265358979323846

typedef long SDWORD;
typedef long long SQWORD;
typedef unsigned long DWORD;
typedef unsigned long long int QWORD;
typedef long long ll;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
int main() {
  int N;
  cin >> N;
  int x[8], y[8];
  REP(i, N) cin >> x[i] >> y[i];

  // idx
  vector<int> idx(N);
  REP(i, N) idx[i] = i;

  // 順列
  double ans = 0;
  do {
    for (int i = 0; i < N - 1; i++) {
      double diffx = x[idx[i]] - x[idx[i + 1]];
      double diffy = y[idx[i]] - y[idx[i + 1]];
      ans += sqrt((pow(diffx, 2) + pow(diffy, 2)));
    }
  } while (next_permutation(idx.begin(), idx.end()));

  // 個数
  double div = 1;
  for (int i = 1; i <= N; i++) {
    div *= i;
  }
  ans = ans / div;
  printf("%lf\n", ans);
  return 0;
}