Sum

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

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;

好吧。。这么写果然还是不怎么完整。。如今补充一下。。

拿第一行 1 2 3来说。。

1 仅仅能由他本身, 即 sum+=A1。  2 能有2, 12。则 sum+=A2+10*A1+A2;   3 能有3, 23 ,123,则:

sum+=A3+10*A2+A3+100*A1+10*A2+A3......合算的sum=111*A1+11*2*A2+1*3*A3;也就是上面说的那个公式。

别忘了计算列的sum.



#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的更多相关文章

  1. 聚合函数:sum,count,max,avg

    聚合函数:sum,count,max,avg等,一般作用于多条记录上.通过group by可以将数据对属于一组的数据起作用. SELECT region, SUM(population), SUM(a ...

  2. LeetCode129:Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

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

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  4. acdream Divide Sum

    Divide Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...

  5. acdream 1431 Sum vs Product

    Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...

  6. Hive函数:SUM,AVG,MIN,MAX

    转自:http://lxw1234.com/archives/2015/04/176.htm,Hive分析窗口函数(一) SUM,AVG,MIN,MAX 之前看到大数据田地有关于max()over(p ...

  7. LeetCode之“树”:Sum Root to Leaf Numbers

    题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...

  8. ACM1001:Sum Problem

    Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.   Input ...

  9. HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)

    Given you a sequence of number a 1, a 2, ..., a n, which is a permutation of 1...n. You need to answ ...

随机推荐

  1. HDU2504 又见GCD

    又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. [LeetCode] Unique Paths 2

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. Delphi访问活动目录

    活动目录Active Directory是用于Windows Server的目录服务,它存储着网络上各种对象的有关信息,并使该信息易于管理员和用户查找及使用.Active Directory使用结构化 ...

  4. Android RingtoneManager铃声管理

    本篇介绍一下跳转到系统铃声选择界面,android中的铃声通过RingtoneManager管理,RingtoneManager管理来电铃声(TYPE_RINGTONE).提示音(TYPE_NOTIF ...

  5. SQL--存储过程+触发器 对比!

    一.存储过程 一:存储过程:存储过程是一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中. 可以用存储过程名字和参数来调用存储过程,这样可以避免代码重复出现,用起来也方便. 例:    下面 ...

  6. 框架学习之道:PE框架简介

    1.PE框架开发新功能所需的部分 2.PE框架工作流程(重要) 首先根据<transcation>中的id号,找到模板(template),然后再根据模板找到责任链(chain),一旦确认 ...

  7. abap alv multiple header using write

    A standard SAP ALV list report will show only one line header, but there will be a requirement somed ...

  8. win7+vs2008+opencv

    1.下载安装VS2008,然后直接下载opencv的windows的安装版, 2.把opencv解压出来,我的路径为:D:\Program\opencv 3.配置PATH:电脑--属性--高级系统设置 ...

  9. Linux几种关机(重启)相关命令

    在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的,通过本文的介绍,希望你可以更加灵活的运用各 ...

  10. 关于JAVA Project.waitfor()死锁问题

    在正常情况下我们可以用Project.waitfor()的返回值是否等于0的方法来判断java调用外部程序是Pass或者是Fail. 但是这个方法往往会被因进程堵塞而导致程序发生死锁,无法再继续执行外 ...