Function Gym - 102801C (规律)

Define function f(x)=∏i=1len(x%10i)%(x+1), where len represents the digit length of x.

For example, f(1023)=(3∗23∗23∗1023)%1024.

Define function g(n,m)={f(g(n,m−1))f(n)m>1m=1. For example, g(n,2)=f(f(n)).

You are given n and m, please calculate ∑i=1mg(n,i).

Input
The input consists of multiple test cases.

The first line contains an integer T (1≤T≤20) — the number of test cases. The description of the test cases follows.

The only line contains two integers n,m (1≤n,m≤109) .

Output
For each test case, print the answer.

Example
Input
2
3 4
4102 642
Output
12
21262

#include<bits/stdc++.h>
using namespace std;
long long f(int n)
{
    if(n==0)
    return 0;
    long long t=10;
    long long ans=1;
    int m=n;
    while(m)
    {
        ans*=n%t;
        ans%=(n+1);
        t*=10;
        m/=10;
    }
    return ans;
}

int main()
{
    long long n,m,t,i;
    scanf("%lld",&t);
    while(t--)
    {
        long long k1,k2,ans=0;
        scanf("%lld %lld",&n,&m);
        k1=f(n);
        ans+=k1;
        for(i=2; i<=m; i++)
        {
            k2=f(k1);
            ans+=k2;
            if(k2==k1)                     //继续下去都会是相同数值,不再进入函数
                break;                     //利用乘法算出答案,节省时间
            else
                k1=k2;
        }
        if(i<m)
            ans+=k1*(m-i);
        if(m==0)
            ans=0;
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值