Hdu 5109 Alexandra and A*B Problem(枚举)

来源:互联网 发布:mac网游排行榜 编辑:程序博客网 时间:2024/05/16 09:06

题目链接

Alexandra and A*B Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 563    Accepted Submission(s): 136


Problem Description
Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given two positive integers A and B, output A*B.
This problem is even easier than the last one. Alexandra can't wait to give him another task: Given a positive integer A and a string S(S only contains numbers.), find the minimum positive integer B, such that S is a substring of T, where T is the decimal notation of A*B.
See the sample for better understanding.
Note: S can contain leading zeros, but T can't.
 

Input
There are multiple test cases (no more than 500). Each case contains a positive integer A and a string S.
A<=10,000,1<=|S|<=8.
 

Output
For each case, output the required B. It is guaranteed that such B always exists.
To C++ programmers: if you want to output 64-bit integers, please use "%I64d" specifier or cout.
 

Sample Input
6 896 192 00861 1
 

Sample Output
3250431
 

Source
BestCoder Round #19 

题意:已知数字a,有数字构成的字符串s。求最小的b,使得T=a*b,且s为T的字串。

题解:T一定为 xsy 的模式,设y的长度为p,s的长度为ls,那么((x*10^ls+s)*10^p+y)%a==0

当p确定的情况下,我们可以枚举x,计算出y,若y<10^p,则符合要求,而x的枚举范围是<=a的,p<=5。所以我们可以枚举p,再枚举x即可。

代码如下:

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<string>#include<queue>#include<stack>#include<map>#include<set>#include<stdlib.h>#include<vector>#define inff 0x3fffffff#define nn 6100#define mod 1000000007typedef long long LL;const LL inf64=inff*(LL)inff;using namespace std;int a;char s[10];LL po[20];int main(){    int i,j;    po[0]=1;    for(i=1;i<=10;i++)        po[i]=po[i-1]*10;    LL ix,k;    while(scanf("%d%s",&a,s)!=EOF)    {        int ls=strlen(s);        LL fc=0;        for(i=0;i<ls;i++)        {            fc=fc*10+s[i]-'0';        }        LL ans=inf64;        j=0;        if(s[0]=='0')            j=1;        for(;j<=a;j++)        {            for(i=0;i<=5;i++)            {                ix=(j*po[ls]+fc)*po[i];                k=ix/a;                if(ix%a)                    k++;                if(k*a-ix<po[i])                {                    ans=min(ans,k);                }            }        }        printf("%I64d\n",ans);    }    return 0;}


0 0