51Nod ~ 1770 ~ Number of numbers (analog + find loop section)

Idea: It can be found that there must be a loop section. Simulate the multiplication process, if there is a loop section, it will end, but pay attention to whether the answer is still +1 if the last digit still needs to be carried.

#include <bits/stdc++.h>
using namespace std;
int a, b, d, n;
intmain()
{
    int T;scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d%d", &a, &b, &d, &n);
        int ans=0, pre=-1, g=0;
        for (int i = 0; i < n; i++)
        {
            int temp = a*b + g;
            g = temp / 10;
            int x = temp % 10;
            if (temp == pre)
            {
                if (x == d) ans += n-i;
                break;
            }
            pre = temp;
            if (x == d) ans++;
        }
        if (g != 0 && g == d) ans++;
        printf("%d\n", ans);
    }
    return 0;
}
/*
2
3 3 9 10
3 3 0 10
*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324384090&siteId=291194637