codeforces 117B B. Very Interesting Game(同余模定理+取模导致的循环节)

来源:互联网 发布:嘲讽知乎 编辑:程序博客网 时间:2024/04/28 06:28

题目链接:

codeforces 117B


题目大意:

给出三个数a,b,m,将不大于a,b的两个数连接后得到一个新的数,如果这个新的数能够整除m,那么2赢,否则2赢。

题目分析:

因为取模会导致出现循环节,所以如果给出的数据范围大于mod,那么我们可以只考虑一个循环节,那么我们只需要预处理出b能够通过得到的值,然后枚举A,判断是否能出现一个A不能和任何一个B组合除能被mod整除的情况,那么这个数就是最小解,如果循环节中都没有出现可行解,那么就是误解了。


AC代码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const LL MAX = 1e7+7;const LL xx = 1e9;LL a,b,m;int dp[MAX];char s[12];int main ( ){    while ( ~scanf ( "%lld%lld%lld" , &a , &b , &m ) )    {        memset ( dp , 0 , sizeof ( dp ) );        LL lim = min ( b , m );        for ( int i = 0 ; i <= lim ; i++ )            dp[i%m] = 1;        lim = min ( a ,  m);        LL ans = -1;        for ( LL i = 0 ; i <= lim ; i++ )        {            LL x = i*xx%m;            x = (m-x)%m;            if ( !dp[x] )            {                ans = i;                break;            }        }        if ( ans == -1 ) puts ( "2" );        else         {            printf ( "1 " );            s[9] = 0;            int n = 8;            for ( int i = 0 ; i < 9 ; i++ )                s[i] = '0';            while ( ans )            {                s[n--] = ans%10+48;                ans /= 10;            }            puts ( s );        }    }}
0 0
原创粉丝点击