Sicily 1803. Arithmetic Sequence

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

4
3
39 1
41 -1
5
-1 -1
4 1
10
10 -1
1 1
2
2 2
3 2

Sample Output

4798
999999907
220
26

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值