因为S是T的子串,那么我们可以构建数XSY,那么这个数就是num = (X*10^strlen(S) + S )*10*strlen(Y) + Y,因为num%a == 0 ,所以根据同余模定理,

num%a == ((x%a) * (( 10^strlen(S) + S%a) *10*strlen(Y) )%a + Y%a,

设 mod = ( num - Y%a ) %a;

所以 Y%a = ( a - mod ) %a;

所以只需枚举Y的长度和x%a,此问题得解

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

int main ( )
{
    LL a , s, b;
    char str[10];
    while ( ~scanf ( "%lld" , &a ) )
    {
        scanf ( "%s" , &str );
        LL base = 1;
        int len = strlen ( str );
        if ( len == 1 && str[0] =='0' ) 
        {
            puts ( "0" );
            continue;
        }
        for ( int i = 0 ; i < len ; i++ )
            base *= 10L;
        sscanf ( str , "%I64d" , &s );
        b = -1;
        for ( LL i = 1 ; i <= 10000 ; i *= 10 )
            for ( LL j = (str[0]=='0'?1:0);j < a ; j++ )
            {
                LL temp = ( j*base + s ) * i;
                LL y = ( a - temp%a ) %a;
                if ( y  < i )
                {
                    temp += y;
                    if ( b == -1 || temp < b )
                        b = temp;
                } 
            }
        printf ( "%I64d\n" , b/a); 
    }
}