文章目录
- 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
#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!
数据范围比较大的题要考虑一下是否可以小范围大范围分开讨论。
#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)
{
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;
Problem G. Pot!!
建四棵线段树
#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];
}
Problem I. Base62
大数进制转化
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)) {
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
单调栈,类似最大矩形面积
#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]++;
}
}
}
Problem N. Fibonacci Sequence
签到
#include
using namespace std;
int main(){
printf("1 1 2 3 5\n");
return 0;
}