ACdream 1139(Sum-逆元)

J - Sum

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

SubmitStatus

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

本题暴力会T

所以简化公式

对于同行/列 须要累加的值为 a1*111+a2*22+a3*3

发现规律sum=∑a(10^(n-i+1)-1)/9*i %F

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define MAXN (1000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n;
char a[MAXN][MAXN];
ll p10[MAXN]={0};
ll pow2(ll b)
{
   if (b==1) return 10;
   if (b==0) return 1;
   if (p10[b]) return p10[b];
   ll p=pow2(b/2)%F;
   p=(p*p)%F;
   if (b&1)
   {
       p=(p*10)%F;
   }
   p10[b]=p;
   return p;
}
ll pow2(ll a,ll b)
{
	if (b==1) return a;
	if (b==0) return 1;
	ll p=pow2(a,b/2)%F;
	p=p*p%F;
	if (b&1)
	{
		p=(p*a)%F;
	}
	return p;
}
ll tot[MAXN]={0};
ll mulinv(ll a)
{
	return pow2(a,F-2);
}
int main()
{
//	freopen("sum.in","r",stdin);
//	freopen("sum.out","w",stdout);
	scanf("%d",&n);
	For(i,n)
	{
		scanf("%s",a[i]+1);

	}
	/*
	For(i,n)
	{
		For(j,n) cout<<a[i][j];
		cout<<endl;
	}
	*/
	For(i,n)
	{
		For(j,n) tot[i]+=a[i][j]-‘0‘+a[j][i]-‘0‘;
	}

//	For(i,n) cout<<tot[i]<<endl;

//	cout<<mul(pow2(10,1232),mulinv(pow2(10,1232)))<<endl;
//	cout<<mulinv(9);

	ll c9=mulinv(9);

	For(i,n) p10[i]=pow2(i);

	ll ans=0;
	For(i,n)
	{
		ll t=sub(p10[n-i+1],1),a=tot[i];
		t=mul(t,c9);
		t=mul(a,t);
		ans=add(ans,mul(t,i));
	}
	cout<<ans<<endl;

	return 0;
}
时间: 2024-10-07 00:24:35

ACdream 1139(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,截断前面的内容,然

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

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

ACdream1139 Sum(推公式+逆元求解)

题目链接:http://acdream.info/problem?pid=1139 题意: 给定一个由0~9组成的矩阵,我们求行相邻的组成的数与列相邻的组成的数的和. eg: 123 456 789 第一行组成的数有 1,2,3,12,23,123 第一列组成的数有 1,4,7,12,47,147. 暴力枚举所有的数肯定是不可取的,我们试着总结. 我们发现a[x][y]在行里出现的数对以后和的贡献为   x*a[x][y]sigma(10 ^(n-i)) (k<=x<=n) 同理a[x][y]

Acdream 1171 Matrix sum 上下界费用流

题目链接:点击打开链接 Matrix sum Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description sweet和zero在玩矩阵游戏,sweet画了一个N * M的矩阵,矩阵的每个格子有一个整数.zero给出N个数Ki,和M个数Kj,zero要求sweet选出一些数,满足从第 i 行至少选出了Ki

acdream 1154 Lowbit Sum

先贴代码,以后再写题解... 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 7 typedef long long ll; 8 9 ll s[100]; 10 11 ll init (int n){ 12 if (s[n]) 13 return s[n]; 14 s[n]=init (

ACdream 1148 GCD SUM (久违的莫比乌斯)

题目链接 题意:给出N,M 执行如下程序: long long  ans = 0,ansx = 0,ansy = 0; for(int i = 1; i <= N; i ++) for(int j = 1; j <= M; j ++) if(gcd(i,j) == 1) ans ++,ansx += i,ansy += j; cout << ans << " " << ansx << " " <<