分析:做着感觉像脑筋急转弯一样......因为空间的限制,存不下每一个数,所以用数学方法来解.
设t1=Σai - Σbi = aj - bj,t2=Σi*ai - Σi*bi = j*(aj - bj).j是a,b不相等的位置,t2/t1就是答案了.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int T, n; long long a, b; int main() { scanf("%d", &T); while (T--) { a = b = 0; scanf("%d", &n); for (long long i = 1; i <= n; i++) { long long t; scanf("%lld", &t); a += t; b += i*t; } for (long long i = 1; i <= n; i++) { long long t; scanf("%lld", &t); a -= t; b -= i*t; } if (!a) printf("0 "); else printf("1 %lld ", b / a); } return 0; }