SW练习_二亲数_DP

递归+map保存中间过程

package info.frady.real;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class ErQinshu_D20200710 {//要递归处理下
    //static long[][][] f;//  0保存-1   1保存0  2保存1
    static Map<String,Long> f;
    public static void main(String[] args) throws Exception{
        //System.setIn(new FileInputStream("C:\\Users\\86153\\Desktop\\SW\\二亲数\\test_input.txt"));
        //System.setIn(new FileInputStream("C:\\Users\\86153\\Desktop\\SW\\二亲数\\sample_input.txt"));
        System.setIn(new FileInputStream("C:\\Users\\86153\\Desktop\\SW\\二亲数\\eval_input.txt"));
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(reader.readLine());

        int T=Integer.parseInt(st.nextToken());
        for (int t = 0; t <T ; t++) {
            long start=System.currentTimeMillis();
            st=new StringTokenizer(reader.readLine());
            int K=Integer.parseInt(st.nextToken());//十进制的数字
            K=Math.abs(K);
            int R=Integer.parseInt(st.nextToken());//字符串的长度
            f=new HashMap<String,Long>();
            //f=new long[K+1][R+1][3];//  0保存-1   1保存0  2保存1
            if(K==0){
                System.out.printf("#%d %d\n",(t+1),1);
                continue;
            }

            long count=0;
            for (int i = 1; i <=R ; i++) {
                count=count+p(K,i,0)+p(K,i,1)+p(K,i,2);
            }
            //System.out.println("预算时间"+(System.currentTimeMillis()-start));
            //System.out.println("运算次数 "+f.size());
            System.out.printf("#%d %d\n",(t+1),count);
        }

        reader.close();
    }

    public static long p(int k,int r,int index){

            String key = k + "_" + r + "_" + index;
            if (f.get(key) != null) {
                return f.get(key);
            }
            if (k == 0 || r == 0) {
                f.put(key,0l);
                return 0;
            }
            if (k == 1 && index == 0) {//数字1,长度为r,以-1结尾
                f.put(key,1l);
                return 1;
            }
            if (k == 1 && index == 1) {//数字1,长度为r,以0结尾
                f.put(key,0l);
                return 0;
            }
            if ((k + 1) % 2 == 0) {
                if (index == 0) {
                    f.put(key, p((k + 1) / 2, r - 1, 0) + p((k + 1) / 2, r - 1, 1) + p((k + 1) / 2, r - 1, 2));
                    //c=c+f[k][r][index];
                }

            }
            if ((k) % 2 == 0) {
                if (index == 1) {
                    f.put(key, p((k) / 2, r - 1, 0) + p((k) / 2, r - 1, 1) + p((k) / 2, r - 1, 2));
                    //c=c+f[k][r][index];
                }
            }
            if ((k - 1) % 2 == 0) {
                if (index == 2) {
                    f.put(key, p((k - 1) / 2, r - 1, 0) + p((k - 1) / 2, r - 1, 1) + p((k - 1) / 2, r - 1, 2));
                    // c=c+f[k][r][index];
                }

            }
            if(f.get(key)==null){
                //System.out.printf("%s \n",key);
                f.put(key,0l);
            }
            return f.get(key);

    }
}
预算时间63
运算次数 300
#1 50
预算时间0
运算次数 447
#2 49
预算时间0
运算次数 597
#3 97
预算时间0
运算次数 591
#4 48
预算时间0
运算次数 741
#5 143
预算时间0
运算次数 738
#6 95
预算时间0
运算次数 888
#7 142
预算时间0
运算次数 732
#8 47
预算时间0
运算次数 1029
#9 187
#10 1
预算时间140
运算次数 47853
#11 28779
预算时间0
运算次数 1155
#12 136
预算时间0
运算次数 1311
#13 229
预算时间16
运算次数 3027
#14 517
预算时间0
运算次数 2481
#15 776
预算时间16
运算次数 7392
#16 2873
预算时间16
运算次数 5235
#17 5369
预算时间78
运算次数 18576
#18 9357
预算时间31
运算次数 28584
#19 28949
预算时间78
运算次数 29541
#20 608
预算时间844
运算次数 369768
#21 61342
预算时间266
运算次数 330852
#22 143485
预算时间219
运算次数 259572
#23 286751
预算时间109
运算次数 221493
#24 3598072
预算时间187
运算次数 222078
#25 21931400
预算时间938
运算次数 814971
#26 5943927
预算时间125
运算次数 237471
#27 1142905149
预算时间578
运算次数 313836
#28 40096123
预算时间468
运算次数 607689
#29 87610899
预算时间891
运算次数 777435
#30 37880707

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值