The code below is using the mathmetical permutation calculation, and trying to avoid integer overflow as much as possible.

For input uniquePaths(9,8), OJ says my answer is 6435*4, which is wrong, but in another environment, the output is exactly 6435.

Since OJ doesn't support printf/cout, I cannot debug. Does anyone know anything about OJ environment?

The code below is tested under

_http://www.compileonline.com/compile_cpp_online.php

```
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int uniquePaths(int m, int n) {
int m2 = m-1;
if(m2 == 0) return 1;
int n2 = n-1;
if (n2 == 0) return 1;
int ways = 1;
int limit = (m2 < n2) ? m2 : n2;
int factor = m2+n2-limit+1;
int divider = 1;
int i = 1;
vector<int> factors(limit);
for(i =0 ; i < limit; i++) { factors[i] =m2+n2-i; }
for(i = limit; i >= 1; i--) {
int res = (m2+n2)%i;
if (factors[res] >= i) {
factors[res] /= i;
} else if (factors[res+i] >= i) {
factors[res+i] /= i;
} else {
divider *= i;
}
}
for(i =0; i< limit; i++) {
ways *= factors[i];
if(divider > 1) {
if ( (ways%divider) ==0) {
ways /= divider;
divider = 1;
}
}
}
return ways;
}
};
int main()
{
Solution s;
cout << s.uniquePaths(9,8) << endl;
return 0;
}
```