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

题目链接:

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 );
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值