SRM 555

呜呜呜。。。。最近感觉头脑迟钝啊

255:给你一个01序列,问你最少能将其分成几段,使得每一段都不含前导0且都是5的幂次

一开始我是建了个最短路跑,后来发现两个循环其实就可以搞定了。类似于dp,从前往后更新,没发现一段区间合法就更新当前的dp值

import java.math.*;
import java.util.*;
public class CuttingBitString {
  boolean isPower(long number) {
    long s = 1;
    while(s < number) {
      s *= 5;
    }
    return s == number;
  }
  public int getmin(String S) {
    int n = (int)S.length();
    int [] dp = new int[n + 1] ;
    Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for(int i = 0; i < n; i++) {
      if(dp[i] < Integer.MAX_VALUE && S.charAt(i) == '1') {
        long number = 0;
        for(int j = i; j < n; j++) {
          number = number * 2 + S.charAt(j) - '0';
          if(isPower(number)) {
            dp[j + 1] = Math.min(dp[j + 1], dp[i] + 1) ;
          }
        }
      }
    }
    return dp[n] < Integer.MAX_VALUE ? dp[n] : -1;
  }
  void debug(Object...obj) {
    System.out.println(Arrays.deepToString(obj));
  }
}



// Powered by FileEdit
// Powered by moj 4.18 [modified TZTester]
// Powered by CodeProcessor


555:给你最大为1555 * 1555的一个全0 矩阵,对行操作Rcount次,对列操作Ccount次,一次操作是选择一行或一列,0变1,1变0,

然后要使得最后的矩阵中1的个数为S个,问总的方案数,如果在某一行或一列上的操作次数是不同的,两种方案就算不同方案

很水的一道题,意识到操作两次等于没操作就可以了。

枚举x行是操作过的,y列是操作过的,剩下的Rcount - x,与Ccount - y都要成对成对的放到相应的行或者列,我是预处理了一个dp来做的。

f[i][j]表示j个物品放入i个不同的盒子的方案总数,可以为空(直接组合数也可以,我闲的当疼用dp)

f[i][j] = f[i-1][0] + f[i-1][1] + f[i-1][2]  + .. f[i-1][j];


import java.math.*;
import java.util.*;
public class XorBoard {
  final static int N = 2000;
  final static int mod = 555555555;
  public int count(int H, int W, int Rcount, int Ccount, int S) {
    int [][]f = new int[N][N];
    for(int i = 0; i < N; i++) {
      f[0][i] = 1;
    }
    for(int i = 1; i < N; i++) {
      for(int j = 0; j < N; j++) {
        if(j == 0) {
          f[i][j] = 1;
        } else {
          f[i][j] = f[i][j - 1] + f[i - 1][j];
          if(f[i][j] >= mod) {
            f[i][j] -= mod;
          }
        }
      }
    }
    int [][]c = new int[N][N];
    for(int i = 0; i < N; i++) {
      c[i][0] = c[i][i] = 1;
      for(int j = 1; j < i; j++) {
        c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
        if(c[i][j] >= mod) {
          c[i][j] -= mod;
        }
      }
    }
    int ret = 0;
    for(int x = 0; x <= H && x <= Rcount; x++) { 
      for(int y = 0; y <= W && y <= Ccount; y++) {
        if((Rcount - x) % 2 == 0 && (Ccount - y) % 2 == 0) {
          if(W * x + y * H - 2 * x * y == S) {
            int a = (int)((long)c[H][x] * f[H - 1][(Rcount - x) / 2] % mod);
            int b = (int)((long)c[W][y] * f[W - 1][(Ccount - y) / 2] % mod);
            ret += (int)( (long) a * b % mod);
            if(ret >= mod) ret -= mod;
          }
        }
      }
    }
    return Integer.valueOf(ret);
  }
  void debug(Object...obj) {
    System.out.println(Arrays.deepToString(obj));
  }
}



// Powered by FileEdit
// Powered by moj 4.18 [modified TZTester]
// Powered by CodeProcessor


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值