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

题目链接

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 8 96 19 2 0086 1 1
 

Sample Output
  
  
3 2 5043 1
 

Source

题意:已知数字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 1000000007
typedef 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值