2019ICPC银川区域赛补题

文章目录

  • Problem B. So Easy
  • Problem F. Function!
  • Problem G. Pot!!
  • Problem I. Base62
  • Problem K. Largest Common Submatri
  • Problem N. Fibonacci Sequence

题目

Problem B. So Easy

2019ICPC银川区域赛补题_第1张图片

#include
using namespace std;
typedef long long ll;
const ll MAXN=1e3+10;
ll mat[MAXN][MAXN];
ll px,py;
int main(){
ll n;
scanf("%lld",&n);
for(ll i=1;i<=n;i++){
    for(ll j=1;j<=n;j++){
        scanf("%lld",&mat[i][j]);
        if(mat[i][j]==-1)
            px=i,py=j;
    }
}int ox=px-1>0?px-1:px+1;
    int oy=py-1>0?py-1:py+1;
        printf("%lld\n",mat[ox][py]+mat[px][oy]-mat[ox][oy]);
}

Problem F. Function!

2019ICPC银川区域赛补题_第2张图片
数据范围比较大的题要考虑一下是否可以小范围大范围分开讨论。

#include
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll exgcd(ll a,ll b,ll &x,ll &y)//扩展欧几里得算法
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll ret=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return ret;
}
ll getInv(ll a,ll mod)//求a在mod下的逆元,不存在逆元返回-1
{
    ll x,y;
    ll d=exgcd(a,mod,x,y);
    return d==1?(x%mod+mod)%mod:-1;
}

int main(){
    ll n;
    scanf("%lld",&n);
    ll ans=0;
    ll i;

    for(i=2;i*i<=n;i++){
        __int128 base=i;
        while(base<=n){
        ans=(ans+((n-base+1)%mod)*(i%mod)%mod)%mod;
        base*=i;
        }
    }

    ll k=i;//cout<
    ans=(ans+(((((k+n)%mod)*((n-k+1)%mod))%mod)*((n+1)%mod)%mod)*getInv(2,mod))%mod;
    ans=(ans-((((n%mod)*((n+1)%mod)%mod)*((2*n+1)%mod)%mod)*getInv(6,mod))%mod+mod)%mod;
    k--;
    ans=ans+(((k%mod)*((k+1)%mod)%mod)*((2*k+1)%mod)%mod)*getInv(6,mod)%mod;
    printf("%lld\n",((ans%mod)+mod)%mod);
    return 0;
}

 

Problem G. Pot!!

2019ICPC银川区域赛补题_第3张图片建四棵线段树

#include
#define lson(x) x<<1
#define rson(x) x<<1|1
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 10;
const ll prime[4]={2,3,5,7};
ll n,q;
struct Segment_tree
{
	 ll l, r;
	 ll MAX, lazy;
	 inline void update(ll val){
	 lazy+=val;
	 MAX+=val;
 }
}tree[4][maxn<<2];
void push_up(ll id,ll x) {
	 tree[id][x].MAX = max(tree[id][lson(x)].MAX , 	tree[id][rson(x)].MAX);
}
void push_down(ll id,ll x) {
	 if (!tree[id][x].lazy)
	  return;
	 ll lazy = tree[id][x].lazy;
	 tree[id][lson(x)].lazy += lazy;
	 tree[id][rson(x)].lazy += lazy;
	 tree[id][lson(x)].MAX += lazy;
	 tree[id][rson(x)].MAX += lazy;
	 tree[id][x].lazy = 0;
}
void build(ll id,ll root, ll l, ll r) {
	 tree[id][root].l = l;
	 tree[id][root].r = r;
	 tree[id][root].MAX = tree[id][root].lazy = 0;
	 if (l == r) {
	  tree[id][root].MAX = 0;
	  return;
	 }
	 ll mid = (l + r) >> 1;
	 build(id,lson(root), l, mid);
	 build(id,rson(root), mid + 1, r);
	 push_up(id,root);
}
ll querymax(ll id,ll root, ll l, ll r) {
	 if (tree[id][root].l == l && tree[id][root].r == r) {
	  return tree[id][root].MAX;
	 }
	 push_down(id,root);
	 ll mid = (tree[id][root].l + tree[id][root].r) >> 1;
	 if (r <= mid)
	  return querymax(id,lson(root), l, r);
	 else if (l > mid)
	  return querymax(id,rson(root), l, r);
	 else
	  return max(querymax(id,lson(root), l, mid) , querymax(id,rson(root), mid + 1, r));
}
void update(ll id,ll root, ll l, ll r, ll val) {
	 if (tree[id][root].l == l && tree[id][root].r == r) {
	  tree[id][root].lazy += val;
	  tree[id][root].MAX += val;
	  return;
 }
	 push_down(id,root);
	 ll mid = (tree[id][root].l + tree[id][root].r) >> 1;
	 if (r <= mid)
	  update(id,lson(root), l, r, val);
	 else if (l > mid)
	  update(id,rson(root), l, r, val);
	 else {
	  update(id,lson(root), l, mid,val);
	  update(id,rson(root), mid + 1, r, val);
	 }
	 push_up(id,root);
}
char op[1000];
int main() {
	ll n,q;
 	scanf("%lld%lld",&n,&q);
 	for(ll i=0;i<4;i++){
        build(i,1,1,n);
 	}
 	for(ll i=0;i<q;i++){
        scanf("%s",op);
        if(op[1]=='U'){
            ll l,r,x;
            scanf("%lld%lld%lld",&l,&r,&x);
            for(ll j=0;j<4;j++){
                if(x%prime[j]==0){
                    ll cnt=0;
                    while(x%prime[j]==0){
                        cnt++;
                        x/=prime[j];
                    }
                    //cout<
                    update(j,1,l,r,cnt);
                }
            }
        }
        else{
            ll l,r;
            scanf("%lld%lld",&l,&r);
            ll ans=0;
            for(ll j=0;j<4;j++){
                ans=max(ans,querymax(j,1,l,r));
                //cout<
            }
            printf("ANSWER %lld\n",ans);
        }
 	}
 	return 0;
}

Problem I. Base62

2019ICPC银川区域赛补题_第4张图片
大数进制转化

import java.util.*;
import java.math.*;
public class Main  {
   static char[] CH=new char[62];
   static HashMap<Character,Integer>map=new HashMap<Character,Integer>();
   static void init() {
      
      for(int i=0;i<=9;i++) {
         CH[i]=(char)('0'+i);
         map.put((char)('0'+i), i);
      }
      for(int i=10;i<36;i++) {
         CH[i]=(char)('A'+i-10);
         map.put((char)('A'+i-10),i);
      }
      for(int i=36;i<62;i++) {
         CH[i]=(char)('a'+i-36);
         map.put((char)('a'+i-36),i);
      }
   }
   static String trans(int a,int b,String c) {
      BigInteger num=BigInteger.ZERO;
      for(int i=0;i<c.length();i++) {
         char x=c.charAt(i);
         num=num.multiply(BigInteger.valueOf(a));
         num=num.add(BigInteger.valueOf(map.get(x)));
      }
      StringBuilder s=new StringBuilder();
      if(num.equals(BigInteger.ZERO)){
         s.append('0');
         return s.toString();
      }
      while(!num.equals(BigInteger.ZERO)) {
         //System.out.println(num);
         s.append(CH[num.mod(BigInteger.valueOf(b)).intValue()]);
         num=num.divide(BigInteger.valueOf(b));
      }
      return s.reverse().toString();
   }
   public static void main(String[] args) {
      init();
      Scanner input=new Scanner(System.in);
      int a=input.nextInt();
      int b=input.nextInt();
      String c=input.next();
      System.out.println(trans(a,b,c));
      input.close();
   }
}

Problem K. Largest Common Submatri

2019ICPC银川区域赛补题_第5张图片
单调栈,类似最大矩形面积

#include
#define de(x) cout<<#x<<" = "<
using namespace std;
const int MAXN=1e3+10;
int a[MAXN][MAXN];
int b[MAXN][MAXN];
int wid[MAXN][MAXN];
struct node{
    int x,y;
};
unordered_map<int,node>mp;
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        scanf("%d",&a[i][j]);
    }
}
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        scanf("%d",&b[i][j]);
        mp[b[i][j]]=(node){i,j};
    }
}
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
            if(wid[i][j-1]>1){
                wid[i][j]=wid[i][j-1]-1;
                continue;
            }
        int x=mp[a[i][j]].x;
        int y=mp[a[i][j]].y;
        for(int k=0;k+j<=m&&k+y<=m;k++){
            if(a[i][k+j]!=b[x][k+y]){
                break;
            }
            wid[i][j]++;
        }
    }
}
/*for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        printf("%d ",wid[i][j]);
    }
    cout<
int ans=0;
for(int j=1;j<=m;j++){
    stack<node>st;
    for(int i=1;i<=n;i++){
            if(a[i-1][j]==b[mp[a[i][j]].x-1][mp[a[i][j]].y]){
        if(st.empty()||wid[i][j]>st.top().y)
            st.push((node){1,wid[i][j]});
        else{
            int len=0;
            while((!st.empty())&&wid[i][j]<=st.top().y){
                    st.top().x+=len;
                    len=st.top().x;
                    ans=max(ans,st.top().x*st.top().y);
                    st.pop();
            }
            st.push((node){len+1,wid[i][j]});
        }
    }
    else{
        int len=0;
    while(!st.empty()){
        st.top().x+=len;
        ans=max(ans,st.top().x*st.top().y);
        len=st.top().x;
        st.pop();
    }
    st.push((node){1,wid[i][j]});
    }
    }
    int len=0;
    while(!st.empty()){
        st.top().x+=len;
        ans=max(ans,st.top().x*st.top().y);
        len=st.top().x;
        st.pop();
    }
}
printf("%d\n",ans);
}
/*
3 4
5 6 7 8
1 2 3 4
9 10 11 12
5 6 8 7
1 2 4 3
12 11 10 9
*/

Problem N. Fibonacci Sequence

2019ICPC银川区域赛补题_第6张图片
签到

#include
using namespace std;
	int main(){
	printf("1 1 2 3 5\n");
	return 0;
}

你可能感兴趣的:(刷题之路)