POJ 3340 & HDU 2410 Barbara Bennett ' s Wild Numbers (math)

Source: Internet
Author: User
Tags pow

Topic Links:

pku:http://poj.org/problem?id=3340

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=2410


Description

A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they have the same length, and every non-question mark character I N X is equal to the character in the same position in W (it means so can replace a question mark W ith any digit). For example, 365198 matches the wild number 36?1?8, but 360199, 361028, or 36128 does not. Write A program This reads a wild number W and a number X from input, both of length n, and Det Ermines the number of n-digit numbers that match W and is greater than x.

Input

There is multiple test cases in the input. Each test case consists of lines of the same length. The first line contains a wild number W, and the second line contains an integer number X. The length of input lines is between 1 and characters. The last line of input contains a single character #.

Output

For each test case, write a single line containing the number of n-digit numbers matching W and greater than X (n is the length of W and X).

Sample Input

36?1?82364288?3910?5#

Sample Output

10004

Source

Tehran 2006


Test instructions

Give a string of two numbers, "?" in string one, and ask in '? ' Position fill number total of how many fill in the method, ensure that the string is greater than two!


The code is as follows:

#include <cstdio> #include <cstring> #include <cmath>const int maxn = 17;typedef __int64 ll;int cont; LL Ans;char S1[maxn];char S2[MAXN]; ll POW (ll N, ll K) {    ll s = 1;    for (LL i = 0; i < K; i++)    {        s*=n;    }    return s;} void solve (int len, int k) {for    (int i = 0; i < len; i++)    {        if (s1[i] = = '? ')        {            ans+= (9-(s2[i]-' 0 ')) *pow (,--cont);        }        else if (S1[i] < s2[i])            return;        else if (S1[i] > S2[i])        {            ans+=pow (10,cont);            return;}}}    int main () {while    (~scanf ("%s", S1))    {        ans = 0;        Cont = 0;        if (s1[0] = = ' # ') break            ;        scanf ("%s", S2);        int len = strlen (S1);        for (int i = 0; i < len; i++)        {            if (s1[i] = = '? ')                cont++;        }        Solve (len,0);        printf ("%i64d\n", ans);    }    return 0;}


POJ 3340 & HDU 2410 Barbara Bennett ' s Wild Numbers (math)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.