新手,欢迎交流,剩下D题不知如何下手,望大佬指教。
计蒜客 2020 蓝桥杯大学 B 组省赛模拟赛 (一)题目及解析
原题链接:传送门
思路:
代码如下:
#include
#define R register int
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
int main()
{
ll n=932065482;
ll ans=n;
for(int ab=1;ab*ab<=n;ab++)
{
if(n%ab==0){
ll c=n/ab;
for(int a=1;a*a<=ab;a++)
if(ab%a==0)
ans=min(ans,2*(ab+a*c+ab/a*c));
}
}
cout<<ans<<endl;
return 0;
}
原题链接:传送门
思路:
代码如下:
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int x=7,a=6;
for(int i=1;i<=5;i++)
x+=a++;
System.out.println(x);
}
}
原题链接:传送门
备注:
#include
#define R register int
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
int main()
{
// 7 2 12 5 9 9 8 10 7 10 5 4 5 8 4 4 10 11 3 8 7 8 3 2 1 6 3 9 7 1
ll a[50],b[50];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=1;i<=30;i++) a[i]=read(),b[30-i+1]=a[i];
ll ans1=0,ans2=0;
for(int i=1;i<=30;i++){
ans1+=a[i]/3;
a[i]%=3;
while(a[i]&&a[i+1]&&a[i+2]) ans1++,a[i]--,a[i+1]--,a[i+2]--;
}
for(int i=1;i<=30;i++){
ans2+=b[i]/3;
b[i]%=3;
while(b[i]&&b[i+1]&&b[i+2]) ans2++,b[i]--,b[i+1]--,b[i+2]--;
}
cout<<max(ans1,ans2)<<endl;
return 0;
}
原题链接:传送门
备注:
原题链接:传送门
思路:
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
public class Main {
static int gcd(int a, int b) {
if(b==0) return a;
else return gcd(b,a%b);
}
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int n=cin.nextInt(),k=cin.nextInt();
int gcds[][] =new int[n][n];
for(int i=1;i<n;i++)
for(int j=1;j<n;j++)
if(gcds[i][j]==0&&gcds[j][i]==0)
{
gcds[i][j]=gcd(i,j);
gcds[j][i]=gcds[i][j];
}
long ans=0;
for(int i=1;i<n;i++)
for(int j=1;j<n;j++)
if(gcds[i][j]==1&&i*i+j*j<=k*k)
ans+=(n-i)*(n-j);
ans=ans*2+2*n*(n-1);
System.out.println((long)ans%(1e9+7));
}
}
原题链接:传送门
思路:
#include
#define R register int
#define ll unsigned long long
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int manx=500;
ll hp[manx][manx];
ll hurt[manx][manx];
ll maps[manx][manx];
int dx[8]={-1,-1,-1,0,0,1,1,1},dy[8]={1,0,-1,1,-1,1,0,-1};
int main()
{
ll n=read(),m=read();
ll a=read(),b=read(),c=read();
ll k=read(),w=read();
for(int i=1;i<=k;i++)
for(int j=1;j<=k;j++)
hurt[i][j]=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
maps[i][j]=read();
if(maps[i][j]==1) hp[i][j]=a;
else if(maps[i][j]==2) hp[i][j]=b;
else hp[i][j]=c;
}
ll q=read();
while(q--)
{
ll id=read(),x=read(),y=read();
if(id)
{
for(int i=x-k/2,ii=1;i<=x+k/2,ii<=k;i++,ii++)
for(int j=y-k/2,jj=1;j<=y+k/2,jj<=k;j++,jj++)
{
if(i<=0||j<=0||i>n||j>m) continue;
if(hp[i][j]){
if(hurt[ii][jj]>=hp[i][j])
hp[i][j]=0;
else hp[i][j]-=hurt[ii][jj];
}
}
}
else {
for(int i=x-k/2,ii=1;i<=x+k/2,ii<=k;i++,ii++)
for(int j=y-k/2,jj=1;j<=y+k/2,jj<=k;j++,jj++)
{
if(i<=0||j<=0||i>n||j>m) continue;
if(hp[i][j]){
if(hurt[ii][jj]>=hp[i][j])
hp[i][j]=0;
else hp[i][j]-=hurt[ii][jj];
}
for(int d=0;d<8;d++){
int xx=dx[d]+i,yy=j+dy[d];
if(xx<=0||yy<=0||xx>n||yy>m) continue;
if(hp[xx][yy]){
if(hp[xx][yy]<=w) hp[xx][yy]=0;
else hp[xx][yy]-=w;
}
}
}
}
}
ll res1=0,res2=0,res3=0,ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(maps[i][j]==1)
{
ans+=a-hp[i][j];
if(hp[i][j]==0) res1++; //这里我原本写的是 res1+=a-hp[i][j];
}
else if(maps[i][j]==2){
ans+=b-hp[i][j];
if(hp[i][j]==0) res2++; //这里我原本写的是 res2+=b-hp[i][j];
}
else{
ans+=c-hp[i][j];
if(hp[i][j]==0) res3++; //这里我原本写的是 res3+=c-hp[i][j];
}
}
cout<<res1<<" "<<res2<<" "<<res3<<endl;
cout<<ans<<endl;
return 0;
}
Java代码如下:
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
public class Main {
static int n,m,a,b,c,k,w;
static int maps[][] = new int[500][500];
static int hp[][] = new int[500][500];
static int hurt[][] = new int[51][51];
static int dx[]={-1,-1,-1,0,0,1,1,1};
static int dy[]={1,0,-1,1,-1,1,0,-1};
static void js(int x, int y) {
for(int i=0;i<8;i++) {
int xx=x+dx[i],yy=dy[i]+y;
if(xx<=0||yy<=0||yy>m||xx>n) continue;
if(hp[xx][yy]>0){
if(w>=hp[xx][yy])
hp[xx][yy]=0;
else hp[xx][yy]-=w;
}
}
}
static void gj(int id, int x, int y) {
for(int i=x-k/2,ii=1;i<=x+k/2;i++,ii++)
for(int j=y-k/2,jj=1;j<=y+k/2;j++,jj++)
{
if(i<=0||j<=0||j>m||i>n) continue;
if(id==0) js(i,j);
if(hp[i][j]>0){
if(hurt[ii][jj]>=hp[i][j])
hp[i][j]=0;
else hp[i][j]-=hurt[ii][jj];
}
}
}
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
m=cin.nextInt();
a=cin.nextInt();
b=cin.nextInt();
c=cin.nextInt();
k=cin.nextInt();
w=cin.nextInt();
for(int i=1;i<=k;i++)
for(int j=1;j<=k;j++)
hurt[i][j]=cin.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
maps[i][j]=cin.nextInt();
if(maps[i][j]==1) hp[i][j]=a;
else if(maps[i][j]==2) hp[i][j]=b;
else hp[i][j]=c;
}
int q=cin.nextInt();
while(q-->0) {
int id=cin.nextInt(),x=cin.nextInt(),y=cin.nextInt();
gj(id,x,y);
}
long ans1=0,ans2=0,ans3=0,ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(maps[i][j]==1)
{
ans+=a-hp[i][j];
if(hp[i][j]==0) ans1++;
}
else if(maps[i][j]==2){
ans+=b-hp[i][j];
if(hp[i][j]==0) ans2++;
}
else{
ans+=c-hp[i][j];
if(hp[i][j]==0) ans3++;
}
}
System.out.println(ans1+" "+ans2+" "+ans3);
System.out.println(ans);
}
}
原题链接:传送门
思路:
#include
#define R register int
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int manx=2e5+5;
ll a[manx];
struct node{
ll u,v,w;
}bian[2*manx];
bool cmp(node a, node b){
return a.w<b.w;
}
ll find(ll x)
{
if(a[x]==x) return x;
else return a[x]=find(a[x]);
}
int main()
{
ll n=read(),m=read(),k=read();
for(int i=1;i<=n;i++) a[i]=i;
for(int i=1;i<=m;i++)
bian[i].u=read(),bian[i].v=read(),bian[i].w=read();
sort(bian+1,bian+1+m,cmp);
ll ans=0,index=1;
for(int i=1;i<=m;i++){
ll u=bian[i].u,v=bian[i].v,w=bian[i].w;
ll fu=find(u),fv=find(v);
if(fu==fv) continue;
a[fv]=a[fu];
ans+=w;
if(index++==n-k) break;
}
cout<<ans<<endl;
return 0;
}
原题链接:传送门
思路:
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;
public class Main {
static class Edge{
int v,w,next;
Edge(int v,int w,int next){
this.v=v;
this.w=w;
this.next=next;
}
}
static int manx=1000000+5;
static int mamx=manx;
static int head[][]=new int[2][manx];
static long d[][]=new long[2][manx];
static boolean vis[]=new boolean[manx];
static Edge a[][] = new Edge[2][mamx];
static int cnt[] = new int[manx];
static int n,m,s,k0=0,k1=0;
static void add(int i,int u,int v, int w)
{
int k;
if(i==0) k=k0;
else k=k1;
Edge x=new Edge(v,w,head[i][u]);
a[i][++k]=x;
head[i][u]=k;
if(i==0) k0=k;
else k1=k;
}
static void spfa(int x){
for(int i=1;i<=n;i++) {
d[x][i]=(long) 1e9;
vis[i]=false;
}
Deque<Integer> q=new ArrayDeque<Integer>();
q.add(s);
d[x][s]=0;
vis[s]=true;
while(!q.isEmpty()){
int u=q.poll();
vis[u]=false;
for(int i=head[x][u];i>0;i=a[x][i].next){
int v=a[x][i].v;
int w=a[x][i].w;
if(d[x][v]>d[x][u]+w){
d[x][v]=d[x][u]+w;
if(!vis[v]) {
vis[v]=true;
if(!q.isEmpty()&&d[x][v]>=d[x][q.peek()]) q.addLast(v);
else q.addFirst(v);
}
}
}
}
}
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int t=cin.nextInt();
while(t-->0) {
n=cin.nextInt();
m=cin.nextInt();
k0=0;
k1=0;
s=1;
for(int i=1;i<=n;i++) {
head[0][i]=0;
head[1][i]=0;
}
while(m-->0){
int u=cin.nextInt();
int v=cin.nextInt();
int w=cin.nextInt();
add(0,u,v,w);
add(1,v,u,w);
}
spfa(0);
spfa(1);
long ans=0;
for(int i=2;i<=n;i++)
ans+=d[1][i]+d[0][i];
System.out.println(ans);
}
}
}
C++代码如下: (可AC)
#include
#define ll long long
#define R register int
#define mm make_pair
using namespace std;
const int manx=1e5+5;
const int mamx=manx;
struct node{
int v,next;
ll w;
}a[2][mamx];
ll d[2][manx];
int head[2][manx];
bool vis[manx];
int n,m,s,e,k0=0,k1=0;
void add(int i,int u,int v,int w)
{
int k;
if(i==0) k=k0; else k=k1;
a[i][++k].next=head[i][u];
head[i][u]=k;
a[i][k].v=v;
a[i][k].w=w;
if(i==0) k0=k; else k1=k;
}
void dij(int x)
{
for(int i=1;i<=n;i++) d[x][i]=1e16,vis[i]=0;
priority_queue<pair<ll,int> >q;
d[x][1]=0;
q.push(make_pair(0,1));
while(q.size()){
int u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=1;
for(int i=head[x][u];i;i=a[x][i].next)
{
int v=a[x][i].v,w=a[x][i].w;
if(d[x][v]>d[x][u]+w)
d[x][v]=d[x][u]+w,q.push(make_pair(-d[x][v],v));
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
k0=0,k1=0;
memset(head,0,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(0,u,v,w);
add(1,v,u,w);
}
dij(0),dij(1);
ll ans=0;
for(int i=2;i<=n;i++){
ans+=d[0][i]+d[1][i];
}
cout<<ans<<endl;
}
return 0;
}
原题链接:传送门
思路:
代码如下:
#include<bits/stdc++.h>
#define R register int
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int manx=1005;
struct node{
int x,y;
};
char mps[manx][manx];
int d[manx][manx];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
bool vis[manx][manx];
int nx[105],ny[105];
int tp[manx][manx];
int n,m;
void bfs(){
queue<node>q;
while(q.size()) q.pop();
int sx=1,sy=1;
d[1][1]=0;
while(tp[sx][sy]>0){
int k=tp[sx][sy];
sx=nx[k];
sy=ny[k];
d[sx][sy]=0;
if(mps[sx][sy]=='*') return;
if(sx==1&&sy==1) return;
}
node star;
star.x=sx,star.y=sy;
q.push(star);
while(q.size()){
node a=q.front();
q.pop();
vis[a.x][a.y]=1;
for(int i=0;i<4;i++)
{
int xx=a.x+dx[i],yy=a.y+dy[i];
if(xx>0&&xx<=n&&yy>0&&yy<=m&&mps[xx][yy]=='.'&&
!vis[xx][yy]&&d[xx][yy]>d[a.x][a.y]+1){
int sx=xx,sy=yy;
d[xx][yy]=d[a.x][a.y]+1;
int flag=0;
while(tp[sx][sy]){
int k=tp[sx][sy];
sx=nx[k];
sy=ny[k];
if(mps[sx][sy]=='*') break;
if(sx==xx&&sy==yy) break;
d[sx][sy]=d[xx][yy];
}
if(!flag || ((sx!=xx||sy!=yy)&&mps[sx][sy]!='*'&&
d[sx][sy]>d[xx][yy])){
node b;
b.x=sx,b.y=sy;
q.push(b);
}
}
}
}
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>mps[i][j];
ll p=read(),index=1;
while(p--)
{
ll x=read(),y=read(),xx=read(),yy=read();
nx[index]=xx,ny[index]=yy;
tp[x][y]=index++;
}
ll ex=read(),ey=read();
memset(d,inf,sizeof(d));
bfs();
if(d[ex][ey]==inf) cout<<"No solution"<<endl;
else cout<<d[ex][ey]<<endl;
return 0;
}
原题链接:传送门
思路:
代码如下:
#include
#define R register int
#define ll unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
inline ll read(){
ll s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int manx=2e3+5;
double dp[manx][manx];
bool r[manx],c[manx]; //column列 row 行
int main()
{
ll p=read();
while(p--)
{
ll n=read(),m=read();
memset(r,0,sizeof(r));
memset(c,0,sizeof(c));
memset(dp,0,sizeof(dp));
ll col,row;
for(int i=1;i<=m;i++){
row=read(),col=read();
r[row]=1;
c[col]=1;
}
col=0,row=0;
for(int i=n;i>=1;i--){
if(c[i]) col++;
if(r[i]) row++;
}
for(int i=n;i>=0;i--)
for(int j=n;j>=0;j--)
{
if(i==n&&j==n) continue;
double x=n*n-i*j;
dp[i][j]=i*(n-j)/x*dp[i][j+1]+(n-i)*j/x*dp[i+1][j]+
(n-i)*(n-j)/x*dp[i+1][j+1]+1.0*n*n/x;
}
printf("%0.5lf\n",dp[row][col]);
}
return 0;
}