# Cutting Paper Squares

# Cutting Paper Squares

JimNero009 + 3 comments Make sure you have enough memory to store the numbers ;)

mike_gorczynski + 0 comments Cheers mate. I was absolutely sure i figured it out so WHY IT WAS FAILING.

kinda silly, because it's a math problem, not core Java or Data structures problem.

fheil0815 + 0 comments kinda stupid. you would only need to add 1 line if the data types were correct.

sairajd044 + 0 comments Not a problem in Python

Bibliophage + 0 comments Each time a piece of paper is cut it becomes 2 pieces, adding 1 to the total number of pieces. Therefore to make x pieces from 1 piece there needs to be x-1 cuts, so the final answer is (n*m)-1.

Interestingly, this principle means that the cuts can be made in any order and configuration.

TheCelestial_25 + 0 comments The reason beind mn - 1 can be understood like this. Forget about any grid for a moment, just imagine a simple piece of paper. Any cut on the paper will give rise to a new piece. If you make 1 cut to a single piece, it will give rise to 1 extra piece. If you further make a cut on any of those pieces, your count increases by one. In total, we need mn pieces, given each is 1x1. We don't have to make mn cuts, because there is already one piece at the beginning. So, we make mn - 1 cuts.

n_nenkov + 0 comments Those are not really math problems, but "take-care-of-large-integers" problems !!!

Oh, c'mon you guys !!!

import java.util.*; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println((sc.nextLong())*(sc.nextLong())-1); } }

SquatchingMan + 1 comment I thought I was doing it right, but then needed to change the pre-written int's in main to long int's. c++ for those who run into the same issue.

SHIVUCODE + 0 comments yes... thanks

shell87301 + 0 comments Really??

#include <bits/stdc++.h> using namespace std; int main() { long long int n, m; cin >> n >> m; cout << m*n-1 << endl; return 0; }

atulmshr83 + 0 comments simple c++ code with data storing in as big datatype as long long int.

#include <bits/stdc++.h> using namespace std; unsigned long long int solve(unsigned long long int n, unsigned long long int m){ // Complete this function unsigned long long int row = n; unsigned long long int column = m; unsigned long long int count = (((row-1)*column)+(column-1)); return count; } int main() { unsigned long long int n; unsigned long long int m; cin >> n >> m; unsigned long long int result = solve(n, m); cout << result << endl; return 0; }

K_padaliya + 0 comments I tried recursion. Works fine no memory issues

static long solve(int n, int m) { if(n==1 & m==1) { return 0; } else if((n==1 & m>1)||n>1 & m==1) { if(n>m) return n-1; return m-1; } else return (m-1)+m*solve(1,n); }

Aabhas99 + 0 comments *SOLUTION TO THE PROBLEM:**First , try your level best and then , IF you have any problem :**You can visit the link below to have the solution:*https://github.com/Aabhas99/HackerRank-Solution-To-Algorithms

*GOOD LUCK!!ðŸ˜Š*

arnaudrousselqc + 0 comments I wasn't able to find the easy formula so I solved it with recursion:

def solve(n, m, mem): inmem = mem.get((n,m),None) if inmem: return inmem if n > 1 and m > 1: q1 = solve(n//2,m//2,mem) q2 = solve((n+1)//2,(m+1)//2,mem) q3 = solve((n+1)//2,m//2,mem) q4 = solve(n//2,(m+1)//2,mem) mem[(n,m)]=q1+q2+q3+q4+3 return q1+q2+q3+q4+3 mem[(n,m)]=max(n-1,m-1) return max(n-1,m-1)

Sort 49 Discussions, By:

Please Login in order to post a comment