Your equation is correct. To carry out the changes reversibly (in order to determine the entropy change of the system), you need to separate the two solids and subject each of them separately and reversibly to the same changes in temperature in different alternate processes. Those alternate reversible processes lead to the same formulas you stated.