kattis ICPC tutorial c++

so i'm working on the kattis problem ICPC tutorial for my cs3 class but since everythings online now i dont really have anyone to ask. but i can't for the life of me figure out why it's accepting only 10/16 test cases i've been going through them for hours and every time i try to change things i get more test cases wrong so i dont know which of my switch cases is the problem. any help would be much appreciated. my 2nd and 3rd asserts also fail but i'm less concerned about those. thank you in advance.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208`` ``````#include #include #include #include using namespace std; int mainSwitch(); //string testSwitch(int, int, int); //void test(); int main() { mainSwitch(); //test(); return 0; } int mainSwitch() { int m, n, t; cin >> m >> n >> t; int temp = n; int factorial = 1; switch (t) { case 1: for (int i = n; i > 0; i--) { if (factorial > m) { cout << "TLE" << endl; return 0; } factorial *= i; } //cout << factorial << endl; break; case 2: for (int i = 1; i < n; i++) { temp *= n; if (temp > m) { cout << "TLE" << endl; return 0; } } cout << temp << endl; break; case 3: for (int i = 1; i < 4; i++) { temp *= n; if (temp > m) { cout << "TLE" << endl; return 0; } } break; case 4: for (int i = 1; i < 3; i++) { temp *= n; if (temp > m) { cout << "TLE" << endl; return 0; } } break; case 5: for (int i = 1; i < 2; i++) { temp *= n; if (temp > m) { cout << "TLE" << endl; return 0; } } break; case 6: if ((n * log2(n)) > m) { cout << "TLE" << endl; return 0; } break; case 7: if (n > m) { cout << "TLE" << endl; return 0; } break; } cout << "AC" << endl; } string testSwitch(int x, int y, int z) { int temp = y; int factorial = 1; string ans; switch (z) { case 1: for (int i = y; i > 0; i--) { if (factorial > x) { ans = "TLE"; } factorial *= i; } break; case 2: for (int i = 1; i < y; i++) { temp *= y; if (temp > x) { ans = "TLE"; } } break; case 3: for (int i = 1; i < 4; i++) { temp *= y; if (temp > x) { ans = "TLE"; } } break; case 4: for (int i = 1; i < 3; i++) { temp *= y; if (temp > x) { ans = "TLE"; } } break; case 5: for (int i = 1; i < 2; i++) { temp *= y; if (temp > x) { ans = "TLE"; } } break; case 6: if ((y * log2(y)) > x) { ans = "TLE"; } break; case 7: if (y > x) { ans = "TLE"; } break; ans = "AC"; } return ans; } void test() { int x, y1, y2, y3, z1, z2, z3; x = 100000000; y1 = 75; y2 = 750; y3 = 1500; z1 = 1; z2 = 6; z3 = 4; assert(testSwitch(x, y1, z1) == "TLE"); assert(testSwitch(x, y2, z2) == "AC"); assert(testSwitch(x, y3, z3) == "AC"); cout << "all tests passed" << endl; }``````
 so i'm working on the kattis problem ICPC tutorial

Maybe you are ... but I don't think anyone else is ... so perhaps you could tell us what the "kattis problem ICPC tutorial" actually is.

It would also help if you removed all the code that is NOT being used, so people can avoid having to hunt through irrelevant material.

Be aware that factorials get very large very fast and will soon exceed the capacity of an int.
Last edited on
Hello TheJast,

Here is another tutorial for you.

Give your variables a proper name and not a single letter that only you know what it means. This is mostly for your benefit, but is does help others who read your code because they do not have to waste time trying to figure out what some of the variables mean.

 ``1234`` ``````int mainSwitch() { int m, n, t; cin >> m >> n >> t;``````

This works until someone runs the program and does not have access to the code to see what needs to be entered. You know what to enter because you wrote the program, but no one else does. You need a prompt before the "cin" to let the user know what to enter and how to enter it.

A few blank lines also helps. As an example:
 ``1234567`` ``````int mainSwitch() { int m, n, t; cin >> m >> n >> t; int temp = n; int factorial = 1; switch (t)``````

 ``123456789101112`` ``````int mainSwitch() { int m, n, t; // <--- Needs a prompt. cin >> m >> n >> t; int temp = n; int factorial = 1; switch (t)``````

One last point. With the lines:
 ``12`` `````` // <--- Needs a prompt. cin >> m >> n >> t;``````

Provide numbers that work and shat the expected output should be. Then for the problem numbers show what does not work.

Something to work on while i get the program loaded and see what happens.

Andy
Thank you, here's a link to the kattis problem https://open.kattis.com/problems/tutorial
I would normally use variables with better names, but the teacher who assigned it specifically said to use the variables from the problem. i understand why that could be confusing though. i normally get to go to the tutoring center and can explain what the variables are. but for future posts i'll make changes before posting. thank guys for the help. i really appreciate it.
m is the number of operations the computer makes in one second n is the number of operations in big o algorithm and t is which case in the switch it is. it's supposed to measure whether or not o(n) operations would exceed the time limit doing m actions for each case (t).
All you have to do is decide if
ft(n) <= m
where ft is whatever function corresponds to type t in the table in your link. If true you output "AC", if not you write "TLE".

That's it!

No assert statements ... just answer the question by writing one of those two things.

- vastly simplify your code to remove all the stuff that you aren't using;
- remove the "assert" statements - they are completely irrelevant.
thank you i'll try to look at it from that perspective. i have to keep the 3 asserts, it's a requirement for the class not the kattis problem. they're worth 30 % of the programs total grade.
 ``123456789101112131415161718192021222324252627`` ``````int main(){ int m, n, t; cin >> m >> n >> t; if (under_limit(m, n, t)) cout << "AC\n"; else cout << "TLE\n"; } bool under_limit(int limit, int n, int type){ switch(type){ case 1: return factorial(limit, n); case 2: return exp2_to_n(limit, n); case 3: return power(limit, n, 4); case 4: return power(limit, n, 3); case 5: return power(limit, n, 2); case 6: return nlgn(limit, n); case 7: return constant(limit, n); } }``````
use that skeleton, it will be easier to analyse each case separately
be careful with overflow and rounding

¿what's the difference between mainSwith() and testSwitch()?
Last edited on
the testSwitch just doesn't have a cin for my asserts. it might not be perfect, i've been trying to teach myself cassert my cs2 class didn't teach asserts and my cs3 teacher expects them on everything but didn't teach them. I can't figure out how to run my asserts through my main switch with the cin.
The assert statements are just to check specific answers.

Ignore them to start with - they are irrelevant to the actual problem. You can soon see whether your code outputs TLE or AC, can't you? You can put assert statements in - if you really have to - once everything is working.
i did that, i did all of the switch originally in main, it all seems like it's working it out puts correctly. i was cout'ing the values to check them and they were correct. but when i run it through kattis it gets stuck on test case 11 but doesn't tell me what that is so i dont know where the problem is. everything i've tried to change has made me fail earlier in the kattis test cases. atleast one of my switch cases is wrong but i can't find it. after i spent the last few days trying to trouble shoot that i gave up and made my asserts so that i'd atleast get partial credit for them. posting on here was something i did as a last ditch effort to figure it out. my programs due in a few hours.
Well, looking at mainswitch() (no idea whether testswitch() is different):
- case 1 would give you n * n!, not n!
- case 2 would give you nn, not 2n
etc.

Why don't you check by hand?
i think it has to be my switch case 2 thats wrong. but when i change it i get wrong answers on kattis earlier. i feel like it should be pow(2, n) that outputs the correct value but kattis seems to think it's wrong.
case 1 is correct for factorials i think factorial 10 is 3628800, and that what i get when i cout the value. i've changed case 2 got rid of the for loop and just made it

 ``12345678910111213141516171819202122`` ``````switch (t) { case 1: for (int i = n; i > 0; i--) { if (factorial > m) { cout << "TLE" << endl; return 0; } factorial *= i; } break; case 2: temp = pow(2, n); if (temp > m) { cout << "TLE" << endl; return 0; } break;``````

which i is outputting the correct value but immedietly fails kattis
Many of these calculations are likely to overflow an int. You should take steps to avoid that.

For example,
2n > m
if and only if
n log 2 > log m
but the latter is considerably less likely to overflow.
Thank you for all the help everyone, i solved it. i really appreciate everyone who took the time to assist, seeing it from all the different ways you guys looked at it helped me find the problem, or problems as there were more than a couple.

final code

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181`` ``````/* This program is designed to test big O algorithms and see if when O(n) actions are taken will it exceed the maximum amount of actions the computer can make in one second m depending on which algorithm you use t. */ #include #include #include #include using namespace std; string testSwitch(int, int, int); void test(); int main() { test(); int m, n, t; cin >> m >> n >> t; int temp = n; int factorial = 1; switch (t) { case 1: for (int i = n; i > 0; i--) { if (factorial > m) { cout << "TLE" << endl; return 0; } factorial *= i; } break; case 2: if (pow(2,n) > m) { cout << "TLE" << endl; return 0; } break; case 3: if (pow(n, 4) > m) { cout << "TLE" << endl; return 0; } break; case 4: if (pow(n, 3) > m) { cout << "TLE" << endl; return 0; } break; case 5: if (pow(n, 2) > m) { cout << "TLE" << endl; return 0; } break; case 6: if ((n * log2(n)) > m) { cout << "TLE" << endl; return 0; } break; case 7: if (n > m) { cout << "TLE" << endl; return 0; } break; } cout << "AC" << endl; return 0; } string testSwitch(int x, int y, int z) { int factorial = 1; string ans = "AC"; switch (z) { case 1: for (int i = y; i > 0; i--) { if (factorial > x) { ans = "TLE"; } factorial *= i; } break; case 2: if (pow(2, y) > x) { ans = "TLE"; } break; case 3: if (pow(y, 4) > x) { ans = "TLE"; } break; case 4: if (pow(y, 3) > x) { ans = "TLE"; } break; case 5: if (pow(y, 2) > x) { ans = "TLE"; } break; case 6: if ((y * log2(y)) > x) { ans = "TLE"; } break; case 7: if (y > x) { ans = "TLE"; } break; } return ans; } void test() { int x, y1, y2, y3, z1, z2, z3; x = 100000000; y1 = 31; y2 = 750; y3 = 1500; z1 = 2; z2 = 6; z3 = 4; assert(testSwitch(x, y1, z1) == "TLE"); assert(testSwitch(x, y2, z2) == "AC"); assert(testSwitch(x, y3, z3) == "TLE"); cout << "all tests passed" << endl; } ``````
Topic archived. No new replies allowed.