题解 | #循环汉诺塔#
循环汉诺塔
https://www.nowcoder.com/practice/cdf0808f841748faba058c8ef538c731
总结:
1.本题可以使用动态规划,分为从A到B,从B到C两个状态。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[][] dp = new long[10000001][2]; long mod = 1000000007; dp[1][0] = 1;dp[1][1] = 2; dp[2][0] = 5;dp[2][1]=7; for(int i=3;i<=n;i++){ dp[i][0] = (1+dp[i-1][1]*2)%mod; dp[i][1] = (1+dp[i-1][1]+dp[i-1][0]+1+dp[i-1][1])%mod; } System.out.print(dp[n][0]+" "+dp[n][1]); } }