N-Dimensional Grid

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维空间,统计N维空间中有多少对相邻点。

解题思路

从一维开始思考,每增加一维的变化,推导出公式。
每增加一维,相邻点的总数就变为前面的相邻点的对数乘以该维长度再加上每一维之间新产生的,即前面的点数乘以当前维的长度-1。

代码

#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int x;
        cin >> x;
        for(int i = 0; i < x; i++)
            scanf("%d", &a[i]);
       unsigned long long point = a[0];  //总点数
       unsigned long long ans = a[0] - 1;
        for(int i = 1; i < x; i++)
        {

            ans = ans * a[i] + point * (a[i] - 1);
            point *= a[i];
            ans %= 1000000007;
            point %= 1000000007;
        }
        cout << ans << endl;
    }
    return 0;
}

总结

忘了要取模。

原文地址:https://www.cnblogs.com/wanghn-blog/p/9386119.html

时间: 2024-10-10 02:29:15

N-Dimensional Grid的相关文章

[uva11916] Emoogle Grid (离散对数)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Emoogle Grid  You have to color an MxN ( 1M, N108) two dimensional grid. You will be provided K ( 2K108) different colors to do so. You will also be provided a list of B ( 0B500) list of blo

UVA - 11916 Emoogle Grid (离散对数取模)

You have to color an M x N (1M, N108) two dimensional grid. You will be provided K (2K108) different colors to do so. You will also be provided a list of B (0B500)list of blocked cells of this grid. You cannot color those blocked cells. A cell can be

UVa11916

11916 Emoogle GridYou have to color an M N (1 M;N 108) two dimensional grid. You will be provided K(2 K 108) different colors to do so. You will also be provided a list of B (0 B 500) list ofblocked cells of this grid. You cannot color those blocked

HDU 3360 National Treasures

National Treasures Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 3   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The great hall of th

UVA 784-Maze Exploration(dfs)

Maze Exploration A maze of rectangular rooms is represented on a two dimensional grid as illustrated in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same character which can be any printa

hdu3360National Treasures (最大匹配,拆点法)

National Treasures Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1038 Accepted Submission(s): 364 Problem Description The great hall of the national museum has been robbed few times recently. Ev

885. Spiral Matrix III

On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw

[Solution] 885. Spiral Matrix Ⅲ

Difficulty: Medium Problem On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and colum

[LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw

LeetCode 885. Spiral Matrix III

原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the