博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[1545] New Year 2014(数位DP,现放标程,待看)
阅读量:4039 次
发布时间:2019-05-24

本文共 1740 字,大约阅读时间需要 5 分钟。

1、

2、题目

  • [1545] New Year 2014

  • 时间限制: 2000 ms 内存限制: 262144 K       

  • 问题描述
  • In the New Year 2014, Xiao Ming is thinking about the question: give two integers Nand K, Calculate the number of the numbers of satisfy the following conditions:

    1. It is a positive integer and is not greater than N.

    2. Xor value of its all digital is K.

    For example N = 12, K = 3, the number of satisfy condition is 3 and 12(3 = 3, 1 ^ 2 = 3).

    In order to let Xiao Ming happy in the New Year 2014, can you help him?

  • 输入
  • There are multiple test cases, each test sample contains two positive integers N and K (0 <= N, K < 10 ^ 18), End to file.           
  • 输出
  • For each case, output the number of the numbers of satisfy condition in one line.           
  • 样例输入
  • 12 3999 512354 8
  • 样例输出
  • 276662
  • 提示
  • 来源
  • 宁静致远 @HBMY

     

    3、比赛标程

    简单的数位DP, dp[pos][statu] (statu 为异或值)!

    本题有个trick, k等于0的时候要将答案减一(题目中要求数都为正的).

    #include
    #include
    #include
    using namespace std;#define lint long longint bit[30];lint dp[30][20];lint dfs(int pos,int x,bool limit){ if(pos<0) { if(limit)return 0; else return x==0; } if(!limit&&dp[pos][x]!=-1)return dp[pos][x]; int end=(limit==0)?9:bit[pos]; lint ans=0; for(int i=0;i<=end;++i) { ans+=dfs(pos-1,i^x,limit&&(i==end)); } dp[pos][x]=ans; return ans;}lint solve(lint n,int x){ int len=-1; memset(dp,-1,sizeof(dp)); while(n) { bit[++len]=n%10; n/=10; } return dfs(len,x,1);}int main(){ //freopen("G:\\1.in","r",stdin); //freopen("G:\\1.out","w",stdout); lint n,x; while(cin>>n>>x) { if(x>15) { puts("0"); continue; } lint ans=solve(n+1,x); if(x==0)--ans; printf("%lld\n",ans); } return 0;}

     

转载地址:http://pgddi.baihongyu.com/

你可能感兴趣的文章
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Word Break(python)
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java swing最简单实例(2) 往JFrame里面放一个容器或组件
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>