ACdream: Sum

Sum

Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)

SubmitStatisticNext
Problem

Problem Description

You are given an N*N digit matrix and you can get several horizontal or vertical digit strings from any position.

For example:

123

456

789

In first row, you can get 6 digit strings totally, which are 1,2,3,12,23,123.

In first column, you can get 6 digit strings totally, which are 1,4,7,14,47,147.

We want to get all digit strings from each row and column, and write them on a paper. Now I wonder the sum of all number on the paper if we consider a digit string as a complete decimal number.

Input

The first line contains an integer N. (1 <= N <= 1000)

In the next N lines each line contains a string with N digit.

Output

Output the answer after module 1,000,000,007(1e9+7)。

Sample Input

3
123
456
789

Sample Output

2784

题目主要是导出公式:

如n行n列的每一行的和sum=1111.....111(n个1)*A1+111...111(n-1个1)*2*A2+.........+11*(n-1)*An-1+1*n*An;

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>

#define M 1000000007
#define f1(i, n)  for(int i=1; i<=n; i++)
#define f2(i, n)  for(int i=0; i<n; i++)
#define f3(j, n)  for(int j=0; j<n; j++) 

using namespace std;

char a[1050][1050];
long long int b[1050];

int main()
{
    int n;
    int k=1050;
    f1(i, k)
       b[i]=0;
    f1(i, k)
       for(int j=1; j<=i; j++)
        b[i]=(b[i]*10+1)%M;
    while(~scanf("%d",&n))
    {
        k=n;
        long long int sum=0;
        f2(i, k)
        {
            scanf("%s",&a[i]);
           // f3(j, k)
           for(int j=0; j<n; j++)
                sum=(sum+((((j+1)*((long long)a[i][j]-'0'))*b[n-j])%M))%M;
        }
        //f3(j, k)
        for(int j=0; j<n; j++)
        {
            f2(i, k)
                sum=(sum+((((i+1)*((long long)a[i][j]-'0'))*b[n-i])%M))%M;
        }
        cout<<sum<<endl;
    }

    return 0;
}

ACdream: Sum

时间: 2024-11-03 15:23:04

ACdream: Sum的相关文章

ACDream - Divide Sum

先上题目: Divide Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description long long ans = 0;for(int i = 1; i <= n; i ++)    for(int j = 1; j <= n; j ++)        ans += a[i] / a[j];给出n,a[1]...a[

ACDream - Power Sum

先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description 给出n,m,p,求 (1^m + 2^m + 3^m + 4^m + ... + n^m) % p Input 第一行一个数T( <= 10),表示数据总数 然后每行给出3个数n,m,p(1 <= n <= m <= 10

ACDream - Lowbit Sum

先上题目: C - Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus Problem Description long long ans = 0;for(int i = 1; i <= n; i ++)    ans += lowbit(i)lowbit(i)的意思是将i转化成二进制数之后,只保留最低位的1及其后面的0,截断前面的内容,然

leetcode题目:Sum Root to Leaf Numbers和Longest Consecutive Sequence

题目一: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 /

聚合函数:sum,avg,max,min,count;模糊查询;排序

----聚合函数 --做计算 做统计 对null过滤:null是指不知道什么值,所以无法做计算--sum(参数):统计和--avg(参数):求平均值--max(参数):最大值--min(参数):最小值--count(参数):获取满足条件的记录数--1.获取学员总人数select COUNT(Email) from Student--2.获取最大的年龄值 年龄值越大,年龄越小 年龄值越小,年龄越大select MAX(BornDate) from Studentselect min(BornDat

每日一九度之 题目1038:Sum of Factorials

时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics, meteorology, scienc

leetcode笔记:Sum Root to Leaf Numbers

一. 题目描述 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1

ACdream 1431 Sum vs Product

题目链接:http://115.28.76.232/problem?pid=1431 Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description Peter has just learned mathematics. He learned how to add, and h

LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T