#11.9 团队排位赛round1_Gym - 102801C _C - Function

C - Function

题目链接在此

C - Function
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 <iostream>
#include <stdio.h>

using namespace std;
typedef long long ll;

ll solve(ll n) {
    if (n == 0)
        return 0;
    ll t = 10, ans = 1;
    ll m = n;
    while (m) {
        ans *= n % t;
        ans %= (n + 1);
        t *= 10;
        m /= 10;
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n, m, t, i;
    cin >> t;
    while (t--) {
        ll k1, k2, ans = 0;
        cin >> n >> m;
        k1 = solve(n);
        ans += k1;
        for (i = 2; i <= m; i++) {
            k2 = solve(k1);
            ans += k2;
            if (k2 == k1)
                break;
            else
                k1 = k2;
        }
        if (i < m)
            ans += k1 * (m - i);
        if (m == 0)
            ans = 0;
        cout << ans << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值