ACdream: Sum
2014-07-12 08:29
495 查看
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;
好吧。。这么写果然还是不怎么完整。。现在补充一下。。
拿第一行 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 OJ 1154 Lowbit Sum
- acdream 1431 Sum vs Product
- ACdreamOJ 1154 Lowbit Sum (数位dp)
- ACdream 1431 Sum vs Product
- ACdream 1139(Sum-逆元)
- ACdreamOJ 1154 Lowbit Sum (数字dp)
- ACDream - Sum
- 数位DP ACdream 1154 Lowbit Sum
- acdream Divide Sum
- Sum vs Product(ACdream) —— dfs
- 二分法入门(二)——POJ 3258,2976;HDU 4430;CodeForces 535C;Gym 101194D;ACdream 1066
- AC_Dream 1216 G - Beautiful People
- ACdream oj 1212 New Year Bonus Grant(贪心+拓扑排序)
- Acdream Xor 简单数学
- ACdream 1033 (二分法)
- ACDREAM 01F(并查集)
- (树形DP) acdream 1028
- DP ACdream 喵哈哈村的挑衅
- ACdreamoj1110(多重背包)
- ACdream 1740 - 哗啦啦族的24点游戏 (全排列DFS)