Soln accepted but time is .156 how to reduce to atleast 0.078 and also how to reduce memory usuage? import java.util.*; import java.io.*; public class prob1264{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); int n=sc.nextInt(); int m=sc.nextInt(); int result=n*(m+1); out.print(result); out.close(); } } Just use such construction, I have time 0.093: StreamTokenizer in = new StreamTokenizer(new BufferedReader( new InputStreamReader(System.in))); PrintWriter out = new PrintWriter( new OutputStreamWriter(System.out), true);
in.nextToken(); int n = (int)in.nval; in.nextToken(); int m = (int)in.nval;
out.println(n * (m + 1)); I know the problem! You're using Java. The answer is "N*(M+1)" yeah, it's mega simple problem... Yeah, it's simple but why my answer was wrong? anyone can help? Give me your code and I will help you Edited by author 31.01.2015 13:34 Edited by author 31.01.2015 13:34 why "N*(M+1)" i can't understand pls help "For each integer from 0 to M the function would calculate how many times this number appears in the N-element array..." Let's say N=2, so you have two-element array. Let's M=4, so you have to check whether there is any 0,1,2,3 or 4 (since from 0 to M) in each array... So total lines = 2*(4+1) Hope, it helped. how could 3 or 4 in the n? cause n = 2 so 3 or 4 are not in the array....for this reason why we don't use if else statement to see whether m is bigger than n or not...but we calculate it straightly?...can you please describe? because, a box is given as a clue ..... and you have to solve the problem according to that.. i guess :/ Edited by author 21.01.2019 19:40 помогите понять условие, пожалуйста, буду благодарен In Python 3.0: N = int(input()) M = int(input()) result = N*(M+1) print(result) #It produces runtime error In C: #include <stdio.h> int main(void) { short int n, k; scanf("%4hd%4hd", &n, &k); printf("%hd\n", n * (k+1)); return 0; } # It produces wrong answer for test N7 SHORT_MAX, probably, is 32767. Replace short with long or long long. Edited by author 26.11.2013 10:16 scanf("%4hd%4hd", &n, &k); /// you dont need this scanf("%i %i", &n, &k); // like this we have N element array. And we have (M+1) integers, as numaration starts form 0 to M. so, (M+1) integers will be checked in every element of the array. as, there's N elements in the array, so, final result is N*(M+1). :) ------------------------------------ #include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; cout << n * (m + 1) << '\n'; return 0; } --------------------------------------- I'm getting time limit exceeded using this algorithm, any optimization suggestions? This problem is taking almost a week 8-| total = 0 for i from 1 to N for j from 0 to M total++ print total Edited by author 07.06.2015 11:34 why using loop, just simply, we have N element array. And we have (M+1) integers, as numaration starts form 0 to M. so, (M+1) integers will be checked in every element of the array. as, there's N elements in the array, so, final result is N*(M+1). :) ------------------------------------ #include <iostream> using namespace std; int main() { int n, m;
cin >> n >> m; cout << n * (m + 1) << '\n'; return 0; } --------------------------------------- Edited by author 08.06.2015 22:43 When I submit this problem I get waiting status and I am not the only one. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string[] nums = Console.ReadLine().Split(' '); long n = int.Parse(nums[0]); long m = int.Parse(nums[1]); if (0 <= n && n <= 40000) { if (0 <= m && m <= 40000) { Console.WriteLine(n * (m + 1)); Console.ReadKey(); } } } } } I suggest you to remove "if" statements, because there is no alternative output provided. Also, try to remove Console.ReadKey(); line before posting your code. Right... I guess this problem was put in here for comic relief. :) For crying out loud, the author didn't even bother to increase the limits so as to make this a 'bignum' problem... :P This is "Осеннее первенство школьников 2003". Now you understand why it is so easy? And yet, this problem has a higher site difficulty than Ural Steaks, which is a much more sophisticated problem than this one. The difficulty of a problem is not necessarily related to how much code it takes to solve it, the point of problem solving is to map scenarios to programs that solve them. It is the proper understanding of the problem where a lot of the difficulty (and most of the importance!) lies. this kind of problems are easy,, i guess they exist to build up your confidence. xD I think that complexity 68 is too big for this task and should be significantly reduced. It's counted with formula, so if more authors solve it - it'l become easier. is this dynamic programming ? :) or maybe we could try a nice branch and bound ! ;) Yes!! it's not a dp! this problem is much simpler than you think! answer is n*(m+1). who wants to get explanation, email me rganeyev at gmail.com Oh Really???!!! Isn't it DP?! WOW! BUT HOW?!!! Oh please please DO tell me, how on earth can this extremely sophisticated problem be solved without DP! I read your formula, but I can't understand it. Oh please help. :)))) ))) After many WA I finally got AC. That was one tough problem :D If you about getting 0.015, now you cannot get less because of system aspects, as I know. If in something way it's about problem, it's easy :) var n,m:longint; begin readln(n,m); writeln(n*(m+1)); end. |
|