C 子矩阵问题 51Nod - 1051
一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值。
例如:3*3的矩阵:
-1 3 -1
2 -1 3
-3 1 2
和最大的子矩阵是:
3 -1
-1 3
1 2
Input 第1行:M和N,中间用空格隔开(2 <= M,N <= 500)。
第2 - N + 1行:矩阵中的元素,每行M个数,中间用空格隔开。(-10^9 <= M <= 10^9) Output 输出和的最大值。如果所有数都是负数,就输出0。 Sample Input
3 3 -1 3 -1 2 -1 3 -3 1 2Sample Output
7
大佬的解题代码:
#include<bits/stdc++.h> using namespace std;const int N=507;typedef long long ll; ll sum[N][N],mn,ans,x,tot;int i,j,k,n,m; int main(){ for(scanf("%d%d",&m,&n),i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&sum[i][j]),sum[i][j]+=sum[i][j-1]; for(i=1;i<=m;++i)for(j=i;j<=m;++j){ for(mn=0,x=0,k=1;k<=n;++k)x+=(sum[k][j]-sum[k][i-1]),ans=max(ans,x-mn),mn=min(mn,x); } printf("%lld\n",ans); }
C A^B mod C FZU-1752
Problem Description
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63).
Input
There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 42 10 1000
Sample Output
124
大佬的解题代码:
#include<stdio.h> #define ll long long ll a,b,mod; ll mul(ll a,ll b){ a%=mod;b%=mod; ll c=(long double)a*b/mod; ll ans=a*b-c*mod; return (ans%mod+mod)%mod; } ll pow_mod(ll x,ll n){ ll res=1; while(n){ if(n%2==1)res=mul(res,x); x=mul(x,x); n/=2; } return res; } int main(){ for(;~scanf("%I64d%I64d%I64d",&a,&b,&mod);printf("%I64d\n",pow_mod(a,b))); }