HDU - 6768The Oculus(哈希)

The Oculus

Let’s define the Fibonacci sequence F1,F2,… as F1=1,F2=2,Fi=Fi−1+Fi−2 (i≥3).

It’s well known that every positive integer x has its unique Fibonacci representation (b1,b2,…,bn) such that:

· b1×F1+b2×F2+⋯+bn×Fn=x.

· bn=1, and for each i (1≤i<n), bi∈{0,1} always holds.

· For each i (1≤i<n), bi×bi+1=0 always holds.

For example, 4=(1,0,1), 5=(0,0,0,1), and 20=(0,1,0,1,0,1) because 20=F2+F4+F6=2+5+13.

There are two positive integers A and B written in Fibonacci representation, Skywalkert calculated the product of A and B and written the result C in Fibonacci representation. Assume the Fibonacci representation of C is (b1,b2,…,bn), Little Q then selected a bit k (1≤k<n) such that bk=1 and modified bk to 0.

It is so slow for Skywalkert to calculate the correct result again using Fast Fourier Transform and tedious reduction. Please help Skywalkert to find which bit k was modified.
Input
The first line of the input contains a single integer T (1≤T≤10000), the number of test cases.

For each case, the first line of the input contains the Fibonacci representation of A, the second line contains the Fibonacci representation of B, and the third line contains the Fibonacci representation of modified C.

Each line starts with an integer n, denoting the length of the Fibonacci representation, followed by n integers b1,b2,…,bn, denoting the value of each bit.

It is guaranteed that:

· 1≤|A|,|B|≤1000000.

· 2≤|C|≤|A|+|B|+1.

·∑|A|,∑|B|≤5000000.
Output
For each test case, output a single line containing an integer, the value of k.
Sample Input
1
3 1 0 1
4 0 0 0 1
6 0 1 0 0 0 1
Sample Output
4

题意: 分别给你A,B,C三个数的斐波那契表达式,其中C的表达式中一个1改成了0,你要把那个数字改回来,问你那个数的位置。

题解: 基本思路是求出A*B-C,然后在斐波那契数列里面找是第几个,但是数的长度有1e6,C甚至达到了2e6+1,但我们可以把那些很大的不能存下来的数用哈希存下来,我们就可以采用字符串哈希的思想使用unsigned long long存数,让其自然溢出。

#include<list>
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<iomanip>
#include<cstdlib>
#include<stdexcept>
#include<fstream>
#include<iterator>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e6+10;
const int inf = 0x3f3f3f3f;
const int Base =131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9+9;
#define mem(a,b) memset(a,b,sizeof(a))
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }

ull fi[maxn];//斐波那契数列
void init(){
    fi[1]=1;
    fi[2]=2;
    for(int i=3;i<maxn;i++)//对2e6进行预处理
        fi[i]=fi[i-1]+fi[i-2];
}

int main(){
    init();
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        ull m,A=0,B=0,C=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld",&m);
            if(m)A+=fi[i];
        }
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld",&m);
            if(m)B+=fi[i];
        }
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld",&m);
            if(m)C+=fi[i];
        }
        ull p=A*B-C;
        for(int i=1;i<maxn;i++){
            if(fi[i]==p){
                printf("%d\n",i);
                break;
            }
        }
    }
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值