New Maths(思维,搜索)

题目链接:点击这里

题目大意:
定义了不进位乘法运算 ⊗ ⊗ ,现给出一个 n n n ,判断是否存在整数 a a a ,使之满足 a ⊗ a = n a⊗a=n aa=n

题目分析:
我们发现位数为 x x x 的整数 a a a 进行 a ⊗ a a⊗a aa 运算后答案的位数为 2 x − 1 2x-1 2x1
因为 n n n 的位数小于等于 25 25 25 ,所以 a a a 的位数一定小于等于 ( 25 + 1 ) / 2 = 13 (25+1)/2=13 (25+1)/2=13 ,因此我们考虑直接暴力搜索来解决此问题
此时问题转换成了在搜索最深处判断方案是否合法:
我们可以逐位用 O ( n ) O(n) O(n) 求卷积某一位的方法求出 a ⊗ a a⊗a aa 每一位的结果,并与 n n n 对应位进行比较

具体细节见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 2e5+5;
const int maxm = 3e4+5;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
ll len,ans = Inf,a[maxn],b[maxn];
char s[maxn];
bool check(int i)
{
	ll res = 0;
	for(int j = 0;j <= i;j++) res = (res+b[j]*b[i-j])%10;
	return res == a[i];
}
void dfs(int dep)
{
	if(dep == len)
	{
		ll res = 0,base = 1;
		for(int i = len;i < len+len-1;i++)
			if(!check(i)) return ;
		for(int i = 0;i < len;i++) res = res+b[i]*base,base *= 10;
		ans = min(ans,res);
		return ;
	}
	for(int i = 0;i <= 9;i++)
	{
		b[dep] = i;
		if(check(dep)) dfs(dep+1);
	}
} 
signed main()
{
	scanf("%s",s);
	len = strlen(s);
	if(len%2 == 0) return 0*puts("-1");
	for(int i = 0;i < len;i++) a[len-i-1] = s[i]-'0';
	len = len+1>>1;
	dfs(0);
	printf("%lld\n",ans == Inf ? -1 : ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值