N-Dimensional Grid

166 篇文章 0 订阅

 

 

You are given an n-dimensional grid in which the dimensions of the grid are a1 × a2 × ... × an. Each cell in the grid is represented as an n-tuple (x1, x2, ..., xn) (1 ≤ xi ≤ ai).

Two cells are considered to be adjacent if the Manhattan Distance between them is equal to 1. The Manhattan Distance between two cells X(x1, x2, ..., xn) and Y(y1, y2, ..., yn) is equal to: |x1 - y1| + |x2 - y2| + ... + |xn - yn|.

Your task is to count how many pairs of cells are adjacents. Can you? Two pairs of cells are considered the same if they include the same cells, i.e the pair (c1, c2)is the same as (c2, c1).

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105), in which n is the number of dimensions of the grid. Then a line follows containing n integers a1, ..., an (1 ≤ ai ≤ 105), in which ai is the size of the ith dimension.

The sum of n overall test cases does not exceed 6 × 106.

Output

For each test case, print a single line containing the number of pairs of adjacent cells modulo 109 + 7.

Example

Input

1
3
1 2 3

Output

7

Note

The absolute value |x| of a real number x is the non-negative value of x without regard to its sign. Namely, |x| = x for a positive x, |x| =  - x for a negative x (in which case  - x is positive), and |0| = 0. For example, the absolute value of 3 is 3, and the absolute value of  - 3 is also 3. The absolute value of a number may be thought of as its distance from zero.

题目:比赛时根本就没看这一题  比赛完看答案还是懵逼  被自己菜哭了   还摔了键盘,,

 

题目大意:有N维的空间(改成空间更好理解),给出每一维的大小,计算总的相邻点的个数

思维:代码篇幅中有写。

代码:

#include<bits/stdc++.h> 
using namespace std;
int w,n,a[100005];
/*
	本题的思路是 N维空间
		第一维空间的时候  有sum=a[1]-1个相邻点
		第二维空间的时候   有sum = sum*a[2] + a[1]*(a[2]-1)   (低维的复制a[i]遍,然后是维维(低)之间的点数*低维一共有多少点) 
		。。。。 
*/ 

int main(){
	scanf("%d",&w);
	while(w--){
		scanf("%d",&n);		
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		long long sum=a[1]-1,num=a[1];
		for(int i=2;i<=n;i++){
			sum=sum*a[i];//对数复制(单位内的数量*第I维的size)
			sum%=1000000007;
			sum+=num*(a[i]-1);//维维之间的相邻点计算
			sum%=1000000007;
			num=num*a[i];
			num%=1000000007;
		}
		printf("%lld\n",sum);//注意在进行int的加减时会出现爆int的情况,wa两发,真菜!
	}
	return 0;
} 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值