这道题一开始很懵逼 完全没头绪 ~~~廖某 动动脑筋就像出来了
https://me.csdn.net/weixin_43922043
然后按照他的思路自己实现了一遍
又因为dp[?] 都是存放余?的组合总数 dp[(?+ x )%3 ] = dp[(?+x)%3] + dp[?]; 具体看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int dp [] = new int [3];
for(int i = 0 ; i < str.length() ; i++) {
int temp = str.charAt(i)-'0';
if(temp % 3 == 0) {
dp[0] = (int) ((dp[0]*2 +1)%(1e9+7));
dp[1] = (int) ((dp[1]*2)%(1e9+7));
dp[2] = (int) ((dp[2]*2)%(1e9+7));
}
else if(temp % 3 == 1) {
int t1 =dp[1]+dp[0]+1;
dp[0] =(int) ((dp[0]+ dp[2])%(1e9+7));
dp[2] =(int) ((dp[2]+ dp[1])%(1e9+7));
dp[1] = (int) (t1%(1e9+7));;
}
else if(temp % 3 == 2) {
int t2 =dp[2]+dp[0]+1;
dp[0] =(int) ((dp[0]+ dp[1])%(1e9+7));
dp[1] =(int) ((dp[1]+ dp[2])%(1e9+7));
dp[2] = (int) (t2%(1e9+7));;
}
}
System.out.println(dp[0]);
}
}