Codeforces Round #742 (Div. 2)
题意
正常的加法竖式运算,现在出错了,按照出错的方式,给你一个结果n,找出满足题意的非负整数对的数量。下面为出错方式:
正确方式:
出错方式:
(满10进1时,应该在左边第一位进位,但是错误地在左边第二位进位)
思路:
- 考虑n=12345。我们把它拆成交替的数字。 1 2 ‾ 3 4 ‾ 5 → 135 , 24 1\underline{2}3\underline{4}5 \rightarrow135,24 12345→135,24. 现在的问题相当于找到加起来是135的一对数字乘以加起来是24的一对数字的数量。很明显,每一对都是可行的:例如, 45 + 90 = 135 , 9 ‾ + 15 ‾ = 24 ‾ 45+90=135, \underline{9} + \underline{15} = \underline{24} 45+90=135,9+15=24.
- 有多少种方法可以找到一对总和为n的非负整数?显然有n+1种方法
n + 0 , ( n − 1 ) + 1 , ( n − 2 ) + 2 , ⋯ , 2 + ( n − 2 ) , 1 + ( n − 1 ) , 0 + n . n + 0 , (n - 1) + 1 , (n - 2) + 2 , \cdots,2 + (n - 2) , 1 + (n - 1) , 0 + n. n+0,(n−1)+1,(n−2)+2,⋯,2+(n−2),1+(n−1),0+n. - 假设我们把n分成两个数字a和b,那么答案将是 ( a + 1 ) ( b + 1 ) (a+1)(b+1) (a+1)(b+1),但我们应该减去2,因为这些数字对应于总和中的第一个或第二个数字是0。
- 综上: a n s = ( a + 1 ) ( b + 1 ) − 2 ans = (a + 1)(b +1) - 2 ans=(a+1)(b+1)−2
代码:
代码其实很简单的,主要是不好找规律。
#include<cstdio>
#include<queue>
#include<set>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<unordered_map>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define ull unsigned long long int
#define mt(a,b) memset(a, b, sizeof a)
//#define int long long
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 1e5 + 7, M = 1e6 + 10;
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
string s; cin >> s;
int a = 0, b = 0;
for (int i = 0; i < s.size(); i++)
{
if (i & 1) a = a * 10 + s[i] - '0';
else b = b * 10 + s[i] - '0';
}
cout << (a + 1) * (b + 1) - 2 << endl;
}
return 0;
}