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
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
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.
For each test case, output a single line containing an integer, the value of k.
1
3 1 0 1
4 0 0 0 1
6 0 1 0 0 0 1
4
给定一个两个用斐波那契表示法表示的两个数A和B,以及被改动了一位的用斐波那契表示法表示的数C,C = A * B。这个改动位已知是由1改为0。找出这个被改动的位置k。
假设要修改的这位为k,那么A * B = C + Fk,其中k <= 2e6+1。
假设存在一个P满足F[1] ~ F[2e6+1]模P两两互不相同。
则找到一个k满足:Fk mod P = (A * B - C) mod P即可。
没想到数据范围在2e6以内可以找到模P使得斐波那契数列的所有数不重复。
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
#define rep(i,a,b) for(int i = (a); i <= (b); i ++)
#define per(i,a,b) for(int i = (a); i >= (b); i --)
const int N = 2e6 + 5;
//const int M = 1e6 + 5;
//const int INF = 0x3f3f3f3f;
//const double EPS = 1e-9;
//const double PI = acos(-1);
//const double PI = 3.14;
const ll MOD = 1e9 + 9;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
unsigned long long F[N * 2];
unsigned long long A[N], B[N], C[N * 2];
void init()
{
F[1] = 1;
F[2] = 2;
rep(i,3,N * 2 - 1)
F[i] = F[i-1] + F[i-2];
}
int main()
{
init();
int T;
scanf("%d", &T);
while(T --)
{
int n;
scanf("%d", &n);
rep(i,1,n)
scanf("%llu", &A[i]);
unsigned long long sum1 = 0;
rep(i,1,n)
sum1 += A[i] * F[i];
int m;
scanf("%d", &m);
rep(i,1,m)
scanf("%llu", &B[i]);
unsigned long long sum2 = 0;
rep(i,1,m)
sum2 += B[i] * F[i];
unsigned long long sum = sum1 * sum2;
int t;
scanf("%d", &t);
rep(i,1,t)
scanf("%llu", &C[i]);
unsigned long long sum3 = 0;
rep(i,1,t)
sum3 += C[i] * F[i];
rep(i,1,t)
{
if(sum - sum3 == F[i])
{
printf("%d\n", i);
break;
}
}
}
return 0;
}