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;
}