题目链接:点击这里
题目大意:
定义了不进位乘法运算
⊗
⊗
⊗,现给出一个
n
n
n ,判断是否存在整数
a
a
a ,使之满足
a
⊗
a
=
n
a⊗a=n
a⊗a=n
题目分析:
我们发现位数为
x
x
x 的整数
a
a
a 进行
a
⊗
a
a⊗a
a⊗a 运算后答案的位数为
2
x
−
1
2x-1
2x−1
因为
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
a⊗a 每一位的结果,并与
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;
}