HDU6768 The Oculus(Hash)

Let's define the Fibonacci sequence F1,F2, as F1=1,F2=2,Fi=Fi1+Fi2 (i3).

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 (1i<n), bi{0,1} always holds.

· For each i (1i<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 (1k<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 (1T10000), 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

学习一波QAQ

整体的思路大概就是设modify的是第k位,那么有:A×B=C+Fk,移项后可得:Fk=A×BC,预处理出斐波那契数列后直接查找即可。但是注意到A,B都在1e6级别,这个范围有点大,因此需要找一个模数P,使得Fib[1]~Fib[2e6]这个范围内的元素模P两两不同余(为啥是Fib[2e6]呢?因为k的范围最大到2e6)。题解很巧妙地用了自然溢出,即P取unsigned long long的最大值264

(题目里说不会有连续的1,又注意到这个数列是斐波那契数列,因此这个条件的含义就是一个数不会有其他的表示方法,比如(1110)和(1001)的值是一样的,而前者有连续的1,就不会出现在输入中。但这...会产生影响吗..)

正常的Hash参照大佬博客:https://www.cnblogs.com/stelayuri/p/13367956.html 可以用进阶map也可以用双Hash避免冲突。

#include <bits/stdc++.h>
using namespace std;
unsigned long long fib[2000005], A, B, C, k;
int main()
{
	int t;
	cin >> t;
	fib[1] = 1, fib[2] = 2;
	for(int i = 3; i <= 2000000; i++) fib[i] = fib[i - 1] + fib[i - 2];
	while(t--)
	{
		int na, nb, nc;
		A = B = C = 0;
		cin >> na;
		for(int i = 1; i <= na; i++)
		{
			int temp = 0;
			scanf("%d", &temp);
			if(temp) A += fib[i];
		}
		cin >> nb;
		for(int i = 1; i <= nb; i++)
		{
			int temp = 0;
			scanf("%d", &temp);
			if(temp) B += fib[i];
		}
		cin >> nc;
		for(int i = 1; i <= nc; i++)
		{
			int temp = 0;
			scanf("%d", &temp);
			if(temp) C += fib[i];
		}
		k = A * B - C;
		for(int i = 1; i <= 2000000; i++)
		{
			if(fib[i] == k)
			{
				cout << i << endl;
				break;
			}
		}
	}
	return 0;
 } 
posted @   脂环  阅读(232)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩