Project Euler Homework
|
00001 00018 #include <iostream> 00019 using namespace std; 00020 00021 const int N = 100; 00022 00023 class ncr_million 00025 { 00026 unsigned long x[N][N]; 00027 public: 00028 ncr_million (); 00029 void reset(); 00030 void printAll (); 00031 unsigned long count (); 00032 }; 00033 00034 void ncr_million::reset(){ 00035 unsigned long *begin = &(x[0][0]); 00036 for (unsigned long *p = begin; p-begin < N * N; p++) 00037 *p=0; 00038 } 00039 00040 ncr_million::ncr_million () 00041 { 00042 reset(); 00043 00044 x[0][0] = x[0][1] = 1; 00045 00046 for (int i = 1; i < N; i++) 00047 { 00048 x[i][0] = 1; 00049 for (int j = 1; j <= i; j++) 00050 { 00051 unsigned long temp = x[i - 1][j - 1] + x[i - 1][j]; 00052 if (temp > 1000000) 00053 x[i][j] = 1000001; 00054 else 00055 x[i][j]=temp; 00056 } 00057 x[i][i+1] = 1; 00058 } 00059 } 00060 00061 void 00062 ncr_million::printAll () 00063 { 00064 for (int i = 0; i < N; i++) 00065 { 00066 for (int j = 0; j <= i+1; j++) 00067 cout << x[i][j]; 00068 cout << '\n'; 00069 } 00070 } 00071 00072 unsigned long 00073 ncr_million::count () 00074 { 00075 unsigned long cnt = 0; 00076 unsigned long *begin = &(x[0][0]); 00077 for (unsigned long *p = begin; p-begin < N * N; p++) 00078 if (*p > 1000000) 00079 cnt++; 00080 return cnt; 00081 } 00082 00083 int 00084 main () 00085 { 00086 ncr_million ncr; 00087 //ncr.printAll(); 00088 cout << ncr.count () << endl; 00089 }