C. Carrying Conundrum(找规律)

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 12345135,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,(n1)+1,(n2)+2,,2+(n2),1+(n1),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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

to cling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值