Sicily 1803. Arithmetic Sequence

来源:互联网 发布:淘宝包包店铺名字大全 编辑:程序博客网 时间:2024/04/29 09:41

1803. Arithmetic Sequence

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

    In mathematics, an arithmetic progression (A.P.) or arithmetic sequence is a sequence of numbers such that the difference of any two successive members of the sequence is a constant. For instance, the sequence 3, 5, 7, 9, 11, 13,... is an arithmetic progression with common difference 2. 
    Given two arithmetic sequences a1, a2... an, and b1, b2... bn, create another sequence c1, c2... cn, for each 1<=i<=n, cn = an * bn. Let S = c1+c2+...+cn, please calculate S, modulo 1000000007.

Input

    In the first line of the input is an integer T (1<=T<=50). Then T test cases follow.
    In the first line of each test case is an integer n (1<=n<=1000000000), the length of each sequence. The next line has two integers, a1 and da, it means that for each 1<=i<=n, ai = a1+(i-1)*da. The next line has two integers, b1 and db, it means that for each 1<=i<=n, bi = b1+(i-1)*db. In other words, a1 and b1 are initial item of two arithmetic sequences respectively, and da and db are common difference.(|a1|,|da|,|b1|,|db| <= 1000000000)

Output

    Output one integer for each test case in one line, S, modulo 1000000007.

Sample Input

4339 141 -15-1 -14 11010 -11 122 23 2

Sample Output

479899999990722026

Problem Source

WXYZ

// Problem#: 1803// Submission#: 3341060// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>#include <cmath>using namespace std;const long long MOD = 1000000007;int main() {    std::ios::sync_with_stdio(false);    int caseNum;    cin >> caseNum;    while (caseNum--) {        long long n, a1, da, b1, db;        cin >> n >> a1 >> da >> b1 >> db;        long long p1, p2, p3;        p1 = ((a1 * b1) % MOD+ MOD) % MOD;        p1 = (p1 * n) % MOD;        p2 = ((a1 * db + b1 * da) % MOD + MOD) % MOD;        p2 = (p2 * ((n * (n - 1) / 2) % MOD)) % MOD;        p3 = ((da * db) % MOD + MOD) % MOD;        long long temp = n * (n - 1) / 2;        if (temp % 3 == 0) {            temp = (temp / 3) % MOD;            temp = (((2 * n - 1) % MOD) * temp) % MOD;        } else {            temp %= MOD;            temp = ((((2 * n - 1) / 3) % MOD) * temp) % MOD;        }        p3 = (p3 * temp) % MOD;        cout << (p1 + p2 + p3) % MOD << endl;    }    //getchar();    //getchar();        return 0;}                                 


0 0