题目链接: 06-The Oculus
题意:给出a, b, c的斐波那契01序列,求 c 序列中哪一位错误使得a*b != c
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:
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
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.
The first line of the input contains a single integer T (1≤T≤10000), the number of test cases.
For each case,
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:
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
详见注释
#include
#include
using namespace std;
#pragma GCC optimize(2)
#define Max 1000003
#define ll long long
ll dp[2*Max]; //斐波那契打表
int T, n_a, n_b, n_c, t;
ll _a, _b, _c;
int main()
{
dp[1] = 1;
dp[2] = 2;
for(int i=3; i<Max*2; i++)
dp[i] = dp[i-1] + dp[i-2];
scanf("%d", &T);
while(T--)
{
_a = _b = _c = 0;
scanf("%d", &n_a);
for(int i=1; i<=n_a; i++)
{
scanf("%d", &t);
if(t) _a += dp[i];
}
scanf("%d", &n_b);
for(int i=1; i<=n_b; i++)
{
scanf("%d", &t);
if(t) _b += dp[i];
}
scanf("%d", &n_c);
for(int i=1; i<=n_c; i++)
{
scanf("%d", &t);
if(t) _c += dp[i];
}
ll temp = _a*_b-_c;
for(int i=1; i<=n_a+n_b+1; i++) //遍历dp[i]
{
if(temp == dp[i]) {
printf("%d\n", i);
break;
}
}
}
return 0;
}
蒟蒻一只,欢迎指正