zoukankan      html  css  js  c++  java
  • hdu 5109(构造数+对取模的理解程度)

    Alexandra and A*B Problem

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


    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.
    A10,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
     
    题意:给出一个数字a,以及一个串s(lens<=8)找到一个最小的数字 b ,使得 s 是 a*b = t 的一个连续子序列.
    题解:非常巧妙的题目。假设t = xsy , 那么我们可以写出表达式 t = (x*10^lens+s)*10^len+y ,又因为 t%a == 0 所以对于最小的 t 式子的每一部分,都可以对 a 取模,
    所以我们可以知道 x<a ,因为如果 x >= a,我们可以通过取模操作让 x 变小, 然后如果s[0]==0,那么x的下限就是1,否则x的下限为 0,然后对于 10^len 我们也可以通过同样的道理知道 10^len<= a <= 10^4 ,然后我们化出:
    设 k = (x*10^lens+s)*10^len
    所以 (k+y)%a=0
    y = (-k%a+a)%a (y<10^len)
    所以通过枚举 x,10^len 得到最终的答案。
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    
    int main()
    {
        LL a;
        char s[20];
        while(scanf("%lld%s",&a,s)!=EOF){
            int len = strlen(s);
            if(strcmp(s,"0")==0){
                printf("0
    ");
                continue;
            }
            LL ls = 1,mid=0;
            for(int i=0;i<len;i++) ls*=10;
            for(int i=0;i<len;i++){
                mid = mid*10+s[i]-'0';
            }
            LL ans = -1;
            for(LL i=1;i<=10000;i*=10){
                for(LL j=(s[0]=='0');j<a;j++){
                    LL t = (j*ls+mid)*i;
                    LL y = (a-t%a)%a;
                    if(y>=i) continue;
                    if(ans<0) ans = t+y;
                    else ans = min(t+y,ans);
                }
            }
            printf("%I64d
    ",ans/a);
        }
        return 0;
    }
     
  • 相关阅读:
    CF854C Planning优先队列|set
    暑假集训-8.9总结
    暑假集训-8.6总结
    AcWing 206. 石头游戏 矩阵乘法|矩阵快速幂
    暑假集训-8.5总结
    KMP 和 扩展KMP
    暑假集训-8.4总结
    暑假集训-8.3总结
    2019牛客暑期多校第二场题解FH
    AcWing 196. 质数距离
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5662781.html
Copyright © 2011-2022 走看看