Cluster randomized and multicenter trials sometimes combine two treatments A and B in a factorial design, with conditions such as A, B, A and B, or none. This results in a two-way nested design. The usual issue of sample size and power now arises for various clinically relevant contrast hypotheses. Assuming a fixed total sample size at each level (number of clusters or centers, number of patients), we derive the optimal proportion of the total sample to be allocated to each treatment arm. We consider treatment assignment first at the highest level (cluster randomized trial) and then at the lowest level (multicenter trial). We derive the optimal allocation ratio for various sets of clinically relevant hypotheses. We then evaluate the efficiency of each allocation and show that the popular balanced design is optimal or highly efficient for a range of research questions except for contrasting one treatment arm with all other treatment arms. We finally present simple equations for the total sample size needed to test each effect of interest in a balanced design, as a function of effect size, power and type I error . All results are illustrated on a cluster-randomized trial on smoking prevention in primary schools and on a multicenter trial on lifestyle improvement in general practices.