#include <iostream>
#include <cstring>
using namespace std;
struct OperationCounter
{
int eqTest, ltTest, gtTest;
int mul, div, mod, add, inc;
int assign, deref;
int memAlloc, memFree;
OperationCounter() { reset(); }
void reset() { memset(this, 0, sizeof(*this)); }
void print()
{
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << " equality tests : " << eqTest << endl;
cout << " less than tests : " << ltTest << endl;
cout << "greater than tests : " << gtTest << endl;
cout << " multiplications : " << mul << endl;
cout << " divisions : " << div << endl;
cout << " modulo ops : " << mod << endl;
cout << " additions : " << add << endl;
cout << " increments : " << inc << endl;
cout << " assignments : " << assign << endl;
cout << " dereferences : " << deref << endl;
cout << " allocations : " << memAlloc << endl;
cout << " deallocations : " << memFree << endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
}
} opc1, opc2;
int gcd(int a, int b)
{
opc1.assign += 2;
opc1.eqTest++;
if (b == 0)
return a;
else
{
opc1.mod++;
return gcd(b, a % b);
}
}
int lcm(int a, int b)
{
opc1.assign += 2;
opc1.mul++;
opc1.div++;
return a * b / gcd(a, b);
}
int lcmOfList1(const int * list, int size)
{
opc1.assign ++;
opc1.deref ++;
int ret = list[0];
opc1.assign ++;
for (int i = 1; i < size; ++ i)
{
opc1.ltTest ++;
opc1.inc ++;
opc1.assign ++;
opc1.deref ++;
ret = lcm(ret, list[i]);
}
return ret;
}
int lcmOfList2(const int * list, int size)
{
opc2.memAlloc ++;
int * newList = new int[size];
opc2.assign ++;
for (int i = 0; i < size; ++ i)
{
opc2.ltTest ++;
opc2.inc ++;
opc2.deref += 2;
opc2.assign ++;
newList[i] = list[i];
}
int min, max, imin, cur;
while (true)
{
opc2.assign += 3;
opc2.deref ++;
min = max = newList[0];
imin = 0;
opc2.assign ++;
for (int i = 1; i < size; ++ i)
{
opc2.ltTest ++;
opc2.inc ++;
opc2.assign ++;
opc2.deref ++;
cur = newList[i];
opc2.gtTest ++;
if (min > cur)
{
opc2.assign += 2;
min = cur;
imin = i;
}
opc2.ltTest ++;
if (max < cur)
{
opc2.assign ++;
max = cur;
}
}
opc2.eqTest ++;
if (min == max) break;
opc2.deref +=2 ;
opc2.add ++;
opc2.assign ++;
newList[imin] += list[imin];
}
opc2.deref ++;
opc2.assign ++;
int ret = newList[0];
opc2.memFree ++;
delete[] newList;
return ret;
}
int main()
{
enum { size = 5 };
int nList[size] = { 2, 3, 5, 7, 11 };
cout << "method 1 result : " << lcmOfList1(nList, size) << endl;
cout << "method 1 stats : " << endl;
opc1.print();
cout << endl;
cout << "method 2 result : " << lcmOfList2(nList, size) << endl;
cout << "method 2 stats : " << endl;
opc2.print();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgT3BlcmF0aW9uQ291bnRlcgp7CiAgICBpbnQgZXFUZXN0LCBsdFRlc3QsIGd0VGVzdDsKICAgIGludCBtdWwsIGRpdiwgbW9kLCBhZGQsIGluYzsKICAgIGludCBhc3NpZ24sIGRlcmVmOwogICAgaW50IG1lbUFsbG9jLCBtZW1GcmVlOwoKICAgIE9wZXJhdGlvbkNvdW50ZXIoKSB7IHJlc2V0KCk7IH0KCiAgICB2b2lkIHJlc2V0KCkgeyBtZW1zZXQodGhpcywgMCwgc2l6ZW9mKCp0aGlzKSk7IH0KCiAgICB2b2lkIHByaW50KCkKICAgIHsKICAgICAgICBjb3V0IDw8ICI9LT0tPS09LT0tPS09LT0tPS09LT0tPS09LT0tPS09LT0iIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiICAgIGVxdWFsaXR5IHRlc3RzIDogIiA8PCBlcVRlc3QgICA8PCBlbmRsOwogICAgICAgIGNvdXQgPDwgIiAgIGxlc3MgdGhhbiB0ZXN0cyA6ICIgPDwgbHRUZXN0ICAgPDwgZW5kbDsKICAgICAgICBjb3V0IDw8ICJncmVhdGVyIHRoYW4gdGVzdHMgOiAiIDw8IGd0VGVzdCAgIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiICAgbXVsdGlwbGljYXRpb25zIDogIiA8PCBtdWwgICAgICA8PCBlbmRsOwogICAgICAgIGNvdXQgPDwgIiAgICAgICAgIGRpdmlzaW9ucyA6ICIgPDwgZGl2ICAgICAgPDwgZW5kbDsKICAgICAgICBjb3V0IDw8ICIgICAgICAgIG1vZHVsbyBvcHMgOiAiIDw8IG1vZCAgICAgIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiICAgICAgICAgYWRkaXRpb25zIDogIiA8PCBhZGQgICAgICA8PCBlbmRsOwogICAgICAgIGNvdXQgPDwgIiAgICAgICAgaW5jcmVtZW50cyA6ICIgPDwgaW5jICAgICAgPDwgZW5kbDsKICAgICAgICBjb3V0IDw8ICIgICAgICAgYXNzaWdubWVudHMgOiAiIDw8IGFzc2lnbiAgIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiICAgICAgZGVyZWZlcmVuY2VzIDogIiA8PCBkZXJlZiAgICA8PCBlbmRsOwogICAgICAgIGNvdXQgPDwgIiAgICAgICBhbGxvY2F0aW9ucyA6ICIgPDwgbWVtQWxsb2MgPDwgZW5kbDsKICAgICAgICBjb3V0IDw8ICIgICAgIGRlYWxsb2NhdGlvbnMgOiAiIDw8IG1lbUZyZWUgIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiPS09LT0tPS09LT0tPS09LT0tPS09LT0tPS09LT0tPS09IiA8PCBlbmRsOwogICAgfQoKfSBvcGMxLCBvcGMyOwoKaW50IGdjZChpbnQgYSwgaW50IGIpCnsKICAgIG9wYzEuYXNzaWduICs9IDI7CiAgICBvcGMxLmVxVGVzdCsrOwoKICAgIGlmIChiID09IDApCiAgICAgICAgcmV0dXJuIGE7CiAgICBlbHNlCiAgICB7CiAgICAgICAgb3BjMS5tb2QrKzsKCiAgICAgICAgcmV0dXJuIGdjZChiLCBhICUgYik7CiAgICB9Cn0KCmludCBsY20oaW50IGEsIGludCBiKQp7CiAgICBvcGMxLmFzc2lnbiArPSAyOwoKICAgIG9wYzEubXVsKys7CiAgICBvcGMxLmRpdisrOwoKICAgIHJldHVybiBhICogYiAvIGdjZChhLCBiKTsKfQoKaW50IGxjbU9mTGlzdDEoY29uc3QgaW50ICogbGlzdCwgaW50IHNpemUpCnsKICAgIG9wYzEuYXNzaWduICsrOwogICAgb3BjMS5kZXJlZiArKzsKCiAgICBpbnQgcmV0ID0gbGlzdFswXTsKCiAgICBvcGMxLmFzc2lnbiArKzsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8IHNpemU7ICsrIGkpCiAgICB7CiAgICAgICAgb3BjMS5sdFRlc3QgKys7CiAgICAgICAgb3BjMS5pbmMgKys7CgogICAgICAgIG9wYzEuYXNzaWduICsrOwogICAgICAgIG9wYzEuZGVyZWYgKys7CgogICAgICAgIHJldCA9IGxjbShyZXQsIGxpc3RbaV0pOwogICAgfQoKICAgIHJldHVybiByZXQ7Cn0KCmludCBsY21PZkxpc3QyKGNvbnN0IGludCAqIGxpc3QsIGludCBzaXplKQp7CiAgICBvcGMyLm1lbUFsbG9jICsrOwoKICAgIGludCAqIG5ld0xpc3QgPSBuZXcgaW50W3NpemVdOwoKICAgIG9wYzIuYXNzaWduICsrOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZTsgKysgaSkKICAgIHsKICAgICAgICBvcGMyLmx0VGVzdCArKzsKICAgICAgICBvcGMyLmluYyArKzsKCiAgICAgICAgb3BjMi5kZXJlZiArPSAyOwogICAgICAgIG9wYzIuYXNzaWduICsrOwoKICAgICAgICBuZXdMaXN0W2ldID0gbGlzdFtpXTsKICAgIH0KCiAgICBpbnQgbWluLCBtYXgsIGltaW4sIGN1cjsKCiAgICB3aGlsZSAodHJ1ZSkKICAgIHsKICAgICAgICBvcGMyLmFzc2lnbiArPSAzOwogICAgICAgIG9wYzIuZGVyZWYgKys7CgogICAgICAgIG1pbiA9IG1heCA9IG5ld0xpc3RbMF07CiAgICAgICAgaW1pbiA9IDA7CgogICAgICAgIG9wYzIuYXNzaWduICsrOwoKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IHNpemU7ICsrIGkpCiAgICAgICAgewogICAgICAgICAgICBvcGMyLmx0VGVzdCArKzsKICAgICAgICAgICAgb3BjMi5pbmMgKys7CgogICAgICAgICAgICBvcGMyLmFzc2lnbiArKzsKICAgICAgICAgICAgb3BjMi5kZXJlZiArKzsKCiAgICAgICAgICAgIGN1ciA9IG5ld0xpc3RbaV07CgogICAgICAgICAgICBvcGMyLmd0VGVzdCArKzsKCiAgICAgICAgICAgIGlmIChtaW4gPiBjdXIpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIG9wYzIuYXNzaWduICs9IDI7CgogICAgICAgICAgICAgICAgbWluID0gY3VyOwogICAgICAgICAgICAgICAgaW1pbiA9IGk7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIG9wYzIubHRUZXN0ICsrOwoKICAgICAgICAgICAgaWYgKG1heCA8IGN1cikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgb3BjMi5hc3NpZ24gKys7CgogICAgICAgICAgICAgICAgbWF4ID0gY3VyOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBvcGMyLmVxVGVzdCArKzsKCiAgICAgICAgaWYgKG1pbiA9PSBtYXgpIGJyZWFrOwoKICAgICAgICBvcGMyLmRlcmVmICs9MiA7CiAgICAgICAgb3BjMi5hZGQgKys7CiAgICAgICAgb3BjMi5hc3NpZ24gKys7CgogICAgICAgIG5ld0xpc3RbaW1pbl0gKz0gbGlzdFtpbWluXTsKICAgIH0KCiAgICBvcGMyLmRlcmVmICsrOwogICAgb3BjMi5hc3NpZ24gKys7CgogICAgaW50IHJldCA9IG5ld0xpc3RbMF07CgogICAgb3BjMi5tZW1GcmVlICsrOwoKICAgIGRlbGV0ZVtdIG5ld0xpc3Q7CgogICAgcmV0dXJuIHJldDsKfQoKaW50IG1haW4oKQp7CiAgICBlbnVtIHsgc2l6ZSA9IDUgfTsKCiAgICBpbnQgbkxpc3Rbc2l6ZV0gPSB7IDIsIDMsIDUsIDcsIDExIH07CgogICAgY291dCA8PCAibWV0aG9kIDEgcmVzdWx0IDogIiA8PCBsY21PZkxpc3QxKG5MaXN0LCBzaXplKSA8PCBlbmRsOwogICAgY291dCA8PCAibWV0aG9kIDEgc3RhdHMgIDogIiA8PCBlbmRsOwogICAgb3BjMS5wcmludCgpOwoKICAgIGNvdXQgPDwgZW5kbDsKCiAgICBjb3V0IDw8ICJtZXRob2QgMiByZXN1bHQgOiAiIDw8IGxjbU9mTGlzdDIobkxpc3QsIHNpemUpIDw8IGVuZGw7CiAgICBjb3V0IDw8ICJtZXRob2QgMiBzdGF0cyAgOiAiIDw8IGVuZGw7CiAgICBvcGMyLnByaW50KCk7Cn0=