真的不是黄题的难度。。。。刚开始以为就是个质因子分解的题
import java.io.*;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=4010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
static long prime[]=new long[N];static int cnt=0;
static boolean vis[]=new boolean[N];
public static void init()
{
for(int i=2;i<=4000;i++){
if(!vis[i]){
prime[++cnt]=i;
for(int j=i;j<=4000;j+=i){
vis[j]=true;
}
}
}
}
public static boolean check(long x)
{
long k=(long)Math.pow(x,1.0/2);
if(k*k==x||(k+1)*(k+1)==x)
return true;
k=(long)Math.pow(x,1.0/3);
if(k*k*k==x||(k+1)*(k+1)*(k+1)==x||(k+2)*(k+2)*(k+2)==x)
return true;
return false;
}
public static void main(String[] args) throws IOException
{
init();
int t=cin.nextInt();
while(t-->0)
{
long x=cin.nextLong();
if(check(x)){
out.println("yes");
continue;
}
int flag=1;
for(int i=1;i<=cnt;i++){
if(x%prime[i]==0) {
int g = 0;
while (x % prime[i] == 0) {
g++;
x /= prime[i];
}
if (g == 1) {
flag = 0;
break;
}
}
}
if(flag>0 && check(x))
out.println("yes");
else
out.println("no");
}
out.close();
}
}
会卡精度,两个浮点数相等的条件设置到10的-9次方或者更低。
import java.io.*;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=4010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
static int n,m,k,b[]=new int[105];
static double a[][]=new double[105][105],p[]=new double[105],ans[]=new double[105];
public static void main(String[] args) throws IOException
{
n=cin.nextInt();m=cin.nextInt();
for(int i=1;i<=n;i++) p[i]=cin.nextDouble()*0.01;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) a[i][j]=cin.nextDouble()*0.01;
k=cin.nextInt();
for(int i=1;i<=k;i++){
int x=cin.nextInt();b[x]=1;
}
for(int i=1;i<=n;i++){
ans[i]=p[i];
for(int j=1;j<=m;j++){
if(b[j]>0) ans[i]*=a[i][j];
else ans[i]*=(1-a[i][j]);
}
}
double sum=0;
for(int i=1;i<=n;i++) sum+=ans[i];
List<double[]> e=new ArrayList<>();
for(int i=1;i<=n;i++)
e.add(new double[]{(double) i,ans[i]/sum});
Collections.sort(e, new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
if(Math.abs(o1[1]-o2[1])<1e-9)
return (int) (o1[0]-o2[0]);
if(o1[1]>o2[1]) return -1;
else return 1;
}
});
for(double[] k:e){
System.out.println((int)k[0]+" "+String.format("%.2f",k[1]*100).toString());
}
out.close();
}
}
80分做法,矩阵前缀和+四重循环
import java.io.*;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=4010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
static int n,m,val,a[][]=new int[505][505],sum[][]=new int[505][505];
public static void main(String[] args) throws IOException
{
n=cin.nextInt();m=cin.nextInt();val=cin.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
a[i][j]=cin.nextInt();
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
}
int ans=0,tmp=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int k=0;k<i;k++)
{
for(int g=0;g<j;g++)
{
tmp=sum[i][j]-sum[i][g]-sum[k][j]+sum[k][g];
if(tmp<=val) ans++;
}
}
}
}
System.out.println(ans);
out.close();
}
}
双指针优化一重循环,ac
import java.io.*;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=4010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
static int n,m,val,a[][]=new int[505][505],sum[][]=new int[505][505];
public static int sum(int x1,int y1,int x2,int y2)
{
return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
}
public static void main(String[] args) throws IOException
{
n=cin.nextInt();m=cin.nextInt();val=cin.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
a[i][j]=cin.nextInt();
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
}
long ans=0;
int up=0,down=0,l=0,r=0;
for(up=1;up<=n;up++)
{
for(down=up;down<=n;down++)
{
for(r=1,l=1;r<=m;r++)
{
while(l<=r&&sum(up,l,down,r)>val) l++;
if(l<=r) ans+=r-l+1;
}
}
}
System.out.println(ans);
out.close();
}
}
暴力枚举数组前两项。数组长度最大不超过30(1,1,2,3,5,8……)。
而长度受到起始2项的限制。
import java.io.*;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=100010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
static int n,a[]=new int[N],f[]=new int[N];
public static void main(String[] args) throws IOException
{
n=cin.nextInt();
int mx=0;
for(int i=1;i<=n;i++) {
a[i]=cin.nextInt();
mx=Math.max(mx,(int)Math.log(a[i]));
}
int ans=1000000;
for(int i=1;i<=mx*30;i++)
{
int cnt=0;
f[1]=f[2]=i;
if(f[1]!=a[1]) cnt++;
if(f[2]!=a[2]) cnt++;
for(int j=3;j<=n;j++)
{
f[j]=f[j-1]+f[j-2];
if(f[j]!=a[j]) {
cnt++;
if(cnt>=ans) break;
}
}
ans=Math.min(ans,cnt);
}
out.println(ans);
out.close();
}
}
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=200010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
public static int n,a[]=new int[N];
public static boolean vis[]=new boolean[N];
public static void dfs(int u)
{
if(u==n)
{
for(int i=0;i<n;i++)
out.print(a[i]+" ");
out.println();
return;
}
for(int i=1;i<=n;i++)
{
if(!vis[i]){
a[u]=i;
vis[i]=true;
dfs(u+1);
vis[i]=false;
}
}
}
public static void main(String[] args) throws IOException
{
n=cin.nextInt();
dfs(0);
out.close();
}
}
法一:
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=200010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
public static int n,ans,a[]=new int[N];
public static char c[][]=new char[55][55];
public static boolean col[]=new boolean[N],dg[]=new boolean[N],udg[]=new boolean[N];
public static void dfs(int u)
{
if(u==n)
{
ans++;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++)
out.print(c[i][j]);
out.println();
}
out.println("----------------------");
return;
}
for(int i=0;i<n;i++)
{
if(!col[i]&&!dg[i+u]&&!udg[i-u+n]) {
c[u][i]='Q';
col[i]=dg[i+u]=udg[i-u+n]=true;
dfs(u+1);
c[u][i]='.';
col[i]=dg[i+u]=udg[i-u+n]=false;
}
}
}
public static void main(String[] args) throws IOException
{
n=cin.nextInt();
for(int i=0;i<n;i++) for(int j=0;j<n;j++) c[i][j]='.';
dfs(0);
out.println(ans);
out.close();
}
}
法二:枚举每一个格子
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=200010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
public static int n,ans,a[]=new int[N];
public static char c[][]=new char[55][55];
public static boolean row[]=new boolean[N],col[]=new boolean[N],dg[]=new boolean[N],udg[]=new boolean[N];
public static void dfs(int x,int y,int cnt)
{
if(y==n) {y=0;x++;}
if(x==n)
{
if(cnt==n){
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) out.print(c[i][j]);
out.println();
}
ans++;
out.println("----------------------");
}
return;
}
//不放皇后
dfs(x,y+1,cnt);
// 放皇后
if(!row[x]&&!col[y]&&!dg[x+y]&&!udg[x-y+n])
{
c[x][y]='Q';
row[x]=col[y]=dg[x+y]=udg[x-y+n]=true;
dfs(x,y+1,cnt+1);
row[x]=col[y]=dg[x+y]=udg[x-y+n]=false;
c[x][y]='.';
}
}
public static void main(String[] args) throws IOException
{
n=cin.nextInt();
for(int i=0;i<n;i++) for(int j=0;j<n;j++) c[i][j]='.';
dfs(0,0,0);
out.println(ans);
out.close();
}
}
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static Scanner cin=new Scanner(System.in);
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
static int N=1010;static long INF=Long.MAX_VALUE/2;static long mod=1000000007;
public static int n,m,a[][]=new int[N][N],d[][]=new int[N][N];
public static int dx[]=new int[]{-1,1,0,0};
public static int dy[]=new int[]{0,0,-1,1};
public static int bfs()
{
Queue<int[]> q=new LinkedList<>();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) d[i][j]=-1;
d[0][0]=0;
q.add(new int[]{0,0});
while(!q.isEmpty())
{
int[] tmp=q.peek();
q.poll();
for(int i=0;i<4;i++){
int x=tmp[0]+dx[i],y=tmp[1]+dy[i];
if(x>=0&&x<n&&y>=0&&y<m&&a[x][y]==0&&d[x][y]==-1){
d[x][y]=d[tmp[0]][tmp[1]]+1;
q.add(new int[]{x,y});
}
}
}
return d[n-1][m-1];
}
public static void main(String[] args) throws IOException
{
n=cin.nextInt();
m=cin.nextInt();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
a[i][j]=cin.nextInt();
out.println(bfs());
out.close();
}
}
/*
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
*/