noip模拟赛 whzzt-Confidence

分析:做着感觉像脑筋急转弯一样......因为空间的限制,存不下每一个数,所以用数学方法来解.

设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\n");
        else
            printf("1 %lld\n", b / a);
    }

    return 0;
}

 

posted @ 2017-10-28 16:54  zbtrs  阅读(206)  评论(0编辑  收藏  举报