IGAME

来源:互联网 发布:淘宝介入售后 退货拒收 编辑:程序博客网 时间:2024/06/06 07:29

IGAME - Interesting Game

no tags 

Alice and Bob play an interesting game and the game is played on a number.

So a player, on his chance, can choose any non zero digit of the number and decrease the digit by any non zero amount such that the resulting digit remains non-negative. The player who gets all the digits of the number 0 wins. Both play optimally and Alice starts first. Now tell how many numbers are there between A and B on which if the game is played Alice wins and also find how many numbers are there where Bob wins. On every number between A and B, Alice plays first on that number .

Input Format
The Input line consists of T test cases. On each line there are two numbers A and B.

Output Format
The Output line consists of T lines each having two numbers.

Constraints:
1 ≤ T ≤ 10000
1 ≤ A ≤ B ≤ 1018

Sample Input
2
1 10
101 110

Sample Output
10 0
8 2

Explanation
In the first case the first player Alice will always win because she can reduce any digit to 0.
In the second case the second player Bob will win on 2 numbers 101 and 110. Rest Alice will win.

题目链接:http://www.spoj.com/problems/IGAME/


在Wannafly的每日一题里,放了好久了,我那天把它收藏在QQ里了,发现Wannafly里选的每日一题都不是那么简单,但确实发现选的人用心了,这个题一开始我看的觉得好难,想了半天也没有思路,然后看了题解,发现并没有超出我的知识范围,Nim和数位dp我都会,但是我都没有想到。。。

http://mp.weixin.qq.com/s?__biz=MzIwNDcyMDczMg==&mid=2247483932&idx=4&sn=5774bec322042e8266b5eb4ed3f6e813&chksm=973a9d2fa04d1439bae1ed092385642bde88bf44295433a0b17b3e34634775985916aa1e75fd&mpshare=1&scene=22&srcid=0107vYGBnLzm8rRzHrN6x88y#rd#userconsent#

上面的那个链接是这个题的中文题意。

其实这个题看了题解你就会就得很简单,相当于有n堆石子,他说每一堆(位)都可以取,这就相当于这是个裸地Nim博弈。然后按位考虑,这就是个数位dp了,还是最简单的那种。。。

大牛链接:

http://www.infocool.net/kb/Other/201701/269856.html


代码:

#include <cstdio>#include <cstring>#include <iostream>#define LL long longusing namespace std;int dig[200];LL dp[30][1<<23];int len;LL dfs(int pos,int states,bool limit){//第几位,异或值,是否达到界限(输入的n)    if(pos<1){        if(states==0){            return 1;        }        return 0;    }    if(!limit&&dp[pos][states]!=-1)        return dp[pos][states];    int lit=limit?dig[pos]:9;    LL cnt=0;    for(int i=0;i<=lit;i++){        cnt+=dfs(pos-1,states^i,limit&&(i==lit));    }    if(!limit)        dp[pos][states]=cnt;    return cnt;}LL solve(LL x){    len=0;    while(x){        dig[++len]=x%10;        x/=10;    }    return dfs(len,0,true);}int main(){    int t;    memset(dp,-1,sizeof(dp));    scanf("%d",&t);    while(t--){        LL a,b;        cin>>a>>b;        LL ans1=solve(b)-solve(a-1);        LL ans2=b-a+1-ans1;        cout<<ans2<<" "<<ans1<<endl;    }    return 0;}


0 0