【ICPC PACIFIC NORTHWEST REGION】L.Carry Cam Failure DFS 规律 思维

“Drat!” cursed Charles. “This stupid carry bar is not working in my Engine! I just tried to calculate the square of anumber, but it’s wrong; all of the carries are lost.”“Hmm,” mused Ada, “arithmetic without carries! I wonder if I can figure out what you
摘要由CSDN通过智能技术生成

“Drat!” cursed Charles. “This stupid carry bar is not working in my Engine! I just tried to calculate the square of a
number, but it’s wrong; all of the carries are lost.”
“Hmm,” mused Ada, “arithmetic without carries! I wonder if I can figure out what your original input was, based on
the result I see on the Engine.”
Carryless addition, denoted by ⊕, is the same as normal addition, except any carries are ignored (in base 10). Thus,
37 ⊕ 48 is 75, not 85.
Carryless multiplication, denoted by ⊗, is performed using the schoolboy algorithm for multiplication, column by
column, but the intermediate additions are calculated using carryless addition. More formally, Let amam−1 . . . a1a0
be the digits of a, where a0 is its least significant digit. Similarly define bnbn−1 . . . b1b0 be the digits of b. The digits
of c = a ⊗ b are given by the following equation:
ck = a0bk ⊕ a1bk−1 ⊕ · · · ⊕ ak−1b1 ⊕ akb0,
where any ai or bj is considered zero if i > m or j > n. For example, 9 ⊗ 1 234 is 9 876, 90 ⊗ 1 234 is 98 760, and
99 ⊗ 1 234 is 97 536.
Given N, find the smallest positive integer a such that a ⊗ a = N.
Input
The input consists of a single line with an integer N, with at most 25 digits and no leading zeros.
PacNW 2019—Division 1 Problem L: Carry Cam Failure 25
Output
Print, on a single line, the least positive number a such that a ⊗ a = N. If there is no such a, print ‘-1’ instead.
Examples
Sample Input 1 Sample Output 1
6 4
Sample Input 2 Sample Output 2
149 17
Sample Input 3 Sample Output 3
123476544 11112
Sample Input 4 Sample Output 4
15 -1

题意:定义运算a⊗b为a乘b无进位的结果,现在给一个数n,找到最小的a,使得a⊗a等于n

思路:

看数据范围,肯定是不能枚举每个数了,所以考虑从数位的角度切入。观察到乘的结果c,对于c[k](c的第k位), c[k] = a[1]⊗b[k] + a[2]⊗b[k-1] + … + a[k]⊗[b1],其中每步求和结果对10取余,如图(未体现进位,这里主要图示一下每一位怎么得到的)
在这里插入图片描述
又题目是求a⊗a,那么只用枚举a的每一位就好了,通过上面的c[k],我们知道枚举到当前位pos的数字,这一位对应的乘积结果也可以直接得到(因为公式里面的ab下标都在k以内,已经是得到了的)。每一位从0->9枚举一遍,将得到的c[k]和输入的n的第k位比较,如果相同,就进入下一个数位的枚举,最后将第一次得到的输出即可,即是最小的。
具体细节,枚举的时候,可以知道a的位数肯定是len(n)/2向上取整的,所以就定义len1 = (len(n)+1)/2, 为a的位数,在1<=pos<=len1的时候枚举这个位置上放0->9的情况,在len1<lpos<=len(a)的时候校检得到的乘积对应位和n的对应位是否相等。最后递归出口pos>len的时候输出得到的a数组即可。
时间复杂度O(2m),其中m<=13,时间复杂度可以接受

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值