The first and last terms of an AP are 5 and 45 respectively. If the sum of all its terms is 400, find the common difference and the number of terms.