B君的历史——复数乘法&&爆搜

题意

设 $r = frac{-1+sqrt7 i}{2}$,对任意整数 $x, y$ 都可以找到一个有限的整数集合 $S$,使得

$$x + ysqrt7 i = sum_{k in S }r^k$$

换句话说,就是将 $x + y sqrt7 i$ 转换成 $r$ 进制,集合中不能包含相同的元素,可以证明 $S$ 是唯一的。

输入 $x$ 和 $y$,从小到大输出 $S$ 中的元素。

分析

模拟一下复数乘法,很容易求出 $r$ 的各个次方。(预处理前24项,为什么呢?太大了无法在1s内搜完)

然后,爆搜找到合适的组合构成 $x$ 和 $y$。

#include<bits/stdc++.h>
using namespace std;

double a[30], b[30];
double x, y;

bool res[30];
bool dfs(int t, double ta, double tb)
{
    if(ta == x && tb == y)  return true;
    if(t > 23)  return false;

    if(dfs(t+1, ta, tb))  return true;
    res[t] = true;
    if(dfs(t+1, ta+a[t], tb+b[t]))  return true;
    res[t] = false;

    return false;
}

int  main()
{
    scanf("%lf%lf", &x, &y);
    a[0] = 1, b[0] = 0;
    a[1] = -0.5, b[1] = 0.5;
    for(int i = 2;i <= 23;i++)
    {
        a[i] = (-0.5)*a[i-1] - 3.5*b[i-1];
        b[i] = 0.5*a[i-1] - 0.5*b[i-1];
    }

    dfs(0, 0, 0);

    for(int i = 0;i <= 23;i++)
        if(res[i]) printf("%d ", i);
    printf("
");
}

//题目中 $|x|, |y| leq 10^{18}$,这只能得到部分分

参考链接:https://blog.csdn.net/Blue_CuSO4/article/details/78898370

原文地址:https://www.cnblogs.com/lfri/p/11534687.html