原题链接:https://nanti.jisuanke.com/t/43115
Code:
import java.util.*;
import java.math.*;
public class Main{
static boolean isprime(int n) {
if(n==1 || n==0) return false;
for(int i=2;i*i<=n;i++) {
if(n%i==0)
return false;
}
return true;
}
static boolean check(int n) {
while(n>0) {
if(n%10==5) return true;
n/=10;
}
return false;
}
public static void main(String[] args){
int ans=0;
for(int i=1;i<=100000;i++) {
if(isprime(i) && check(i))
ans++;
}
System.out.println(ans);
}
}
原题链接:https://nanti.jisuanke.com/t/43116
Code:
import java.util.*;
import java.math.*;
public class Main{
static int[] dp = new int[15];
public static void main(String[] args){
dp[0]=1; dp[1]=1;
dp[2]=2; dp[3]=4;
for(int i=4;i<=10;i++) {
if(i==5 || i==7) continue;
dp[i]=dp[i]+dp[i-1]+dp[i-2]+dp[i-3]+dp[i-4];
}
System.out.println(dp[10]);
}
}
原题链接:https://nanti.jisuanke.com/t/43117
Code:
import java.util.*;
import java.math.*;
public class Main{
static int[] f = new int[10];
public static void main(String[] args){
int ans=7;
f[1]=6;
ans+=f[1];
for(int i=2;i<=5;i++) {
f[i]=f[i-1]+1;
ans+=f[i];
}
System.out.println(ans);
}
}
原题链接:https://nanti.jisuanke.com/t/43118
Code:
import java.util.*;
import java.math.*;
public class Main{
static int[] a = new int[35];
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int ans=0;
for(int i=1;i<=30;i++)
a[i]=cin.nextInt();
for(int i=1;i<=30;i++) {
ans+=a[i]/3;
a[i]%=3;
while(a[i]>0 && a[i+1]>0 && a[i+2]>0) {
ans++;
a[i]--;a[i+1]--;a[i+2]--;
}
}
System.out.println(ans);
}
}
原题链接:https://nanti.jisuanke.com/t/43120
Code:
import java.util.Scanner;
import java.util.HashMap;
public class Main{
static long[] f = new long[2000100];
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
HashMap<Long,Long> mp = new HashMap<Long,Long>();
long a = cin.nextLong(), b = cin.nextLong(), c = cin.nextLong();
f[0] = 1;
mp.put(f[0],(long)1);
for(int i=1;i<=2000000;i++) {
f[i] = (f[i-1]*a+f[i-1]%b)%c;
if(mp.get(f[i])!=null) {
System.out.println(i);
return;
}else mp.put(f[i], (long)1);
}
System.out.println(-1);
}
}
原题链接:https://nanti.jisuanke.com/t/43121
Code:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=0x3f3f3f3f;
const int pi=acos(-1.0);
const int mod=1e9+7;
int v[550][550],maps[550][550],life[550][550];
ll sum=0,d_sum=0,f_sum=0,t_sum=0;
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,m; cin>>n>>m;
ll a,b,c; cin>>a>>b>>c;
ll k,w; cin>>k>>w;
for(int i=1;i<=k;i++)
for(int j=1;j<=k;j++)
cin>>v[i][j];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>maps[i][j];
if(maps[i][j]==1) life[i][j]=a;
else if(maps[i][j]==2) life[i][j]=b;
else if(maps[i][j]==3) life[i][j]=c;
}
}
int q; cin>>q;
while(q--){
int id,x,y; cin>>id>>x>>y;
if(id==1){
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 || i>n || j<=0 || j>m) continue;
if(life[i][j]){
if(life[i][j]>v[ii][jj]) life[i][j]-=v[ii][jj];
else life[i][j]=0;
}
}
}
else if(id==0){
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 || i>n || j<=0 || j>m) continue;
if(life[i][j]){
if(life[i][j]>v[ii][jj]) life[i][j]-=v[ii][jj];
else life[i][j]=0;
}
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(life[xx][yy]){
if(life[xx][yy]<=w) life[xx][yy]=0;
else life[xx][yy]-=w;
}
}
}
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(maps[i][j]==1){
if(!life[i][j]) d_sum++;
sum+=(a-life[i][j]);
}
else if(maps[i][j]==2){
if(!life[i][j]) f_sum++;
sum+=(b-life[i][j]);
}
else if(maps[i][j]==3){
if(!life[i][j]) t_sum++;
sum+=(c-life[i][j]);
}
cout<<d_sum<<' '<<f_sum<<' '<<t_sum<<endl;
cout<<sum<<endl;
return 0;
}
原题链接:https://nanti.jisuanke.com/t/43122
Code:
import java.util.Scanner;
public class Main{
static long m;
static long[] a = new long[2010];
static long change(String s) {
long ans=0;
for(int i=0;i<s.length();i++)
ans=(ans*26+s.charAt(i)-'A')%m;
return ans;
}
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
String str = cin.next();
m = cin.nextInt();
int len = str.length();
a[len-1]=1;
for(int i=len-2;i>=0;i--)
a[i]=a[i+1]*26%m;
long ans = change(str);
if(ans==0) {
System.out.println("0 0");
return;
}
for(int i=0;i<len;i++) {
for(int j=i+1;j<len;j++) {
long res = (str.charAt(j)-str.charAt(i))*a[i]+(str.charAt(i)-str.charAt(j))*a[j];
if((ans+res+m)%m==0) { //加上m防止出现负数
System.out.println(++i + " " + ++j); //要用双引号
return;
}
}
}
System.out.println("-1 -1");
}
}
原题链接:https://nanti.jisuanke.com/t/43124
C++ Code:
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int n,m,ex,ey;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int vis[1010][1010],to[1010][1010];
char maps[1010][1010];
struct node{
int x,y,v;
};
struct node1{
int a,b,c,d;
}door[1010];
bool check(int x,int y){
if(x<1 || x>n || y<1 || y>m || maps[x][y]=='*' || vis[x][y])
return false;
return true;
}
int bfs(){
queue<node> q;
node s;
s.x=1,s.y=1,s.v=0;
vis[1][1]=1;
q.push(s);
while(!q.empty()){
node now = q.front();
q.pop();
int flag=0;
if(to[now.x][now.y]){
flag=1;
int t = to[now.x][now.y];
if(check(door[t].c,door[t].d)){
vis[door[t].c][door[t].d]=1;
node next;
next.x=door[t].c, next.y=door[t].d;
next.v=now.v;
if(next.x==ex && next.y==ey) return next.v;
q.push(next);
}
}
if(!flag){
for(int i=0;i<4;i++){
int xx = now.x+dir[i][0];
int yy = now.y+dir[i][1];
if(check(xx,yy)){
vis[xx][yy]=1;
node next;
next.x=xx, next.y=yy, next.v=now.v+1;
if(xx==ex && yy==ey) return next.v;
q.push(next);
}
}
}
}
return -1;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>maps[i][j];
int q; cin>>q;
for(int i=1;i<=q;i++){
cin>>door[i].a>>door[i].b>>door[i].c>>door[i].d;
to[door[i].a][door[i].b]=i;
}
cin>>ex>>ey;
int ans=bfs();
if(ans==-1) cout<<"No solution"<<endl;
else cout<<ans<<endl;
return 0;
}
Java Code:
import java.util.*;
import java.math.*;
class node{
int x,y,v;
public node(){};
public node(int x,int y,int v) {
this.x=x;
this.y=y;
this.v=v;
}
}
class node1{
int a,b,c,d;
public node1(){};
public node1(int a,int b,int c,int d) {
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
}
public class Main {
static int n,m,ex,ey;
static int[][] dir = {{1,0},{-1,0},{0,1},{0,-1}};
static char[][] ch = new char[1010][1010];
static Queue<node> q = new LinkedList<node>();
static node1[] door = new node1[1010];
static int[][] to = new int[1010][1010];
static int[][] vis = new int[1010][1010];
static boolean check(int x,int y) {
if(x<1 || x>n || y<1 || y>m || vis[x][y]==1 || ch[x][y]=='*')
return false;
return true;
}
static int bfs() {
node s = new node(1,1,0);
vis[1][1]=1;
q.add(s);
while(!q.isEmpty()) {
node now = q.poll();
int flag=0;
if(to[now.x][now.y]>0) {
flag=1;
int t = to[now.x][now.y];
if(check(door[t].c,door[t].d)) {
vis[door[t].c][door[t].d]=1;
node next = new node(door[t].c,door[t].d,now.v);
if(next.x==ex && next.y==ey) return next.v;
q.add(next);
}
}
if(flag==0) {
for(int i=0;i<4;i++) {
int xx = now.x + dir[i][0];
int yy = now.y + dir[i][1];
if(check(xx,yy)) {
vis[xx][yy] = 1;
node next = new node(xx,yy,now.v+1);
if(xx==ex && yy==ey) return next.v;
q.add(next);
}
}
}
}
return -1;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt(); m = cin.nextInt();
for(int i=1;i<=n;i++) {
String str = cin.next();
for(int j=0;j<m;j++)
ch[i][j+1] = str.charAt(j);
}
int q = cin.nextInt();
new node(); //初始化类,不然会报空指针异常
for(int i=1;i<=q;i++) //初始化类,不然会报空指针异常
door[i] = new node1();
for(int i=1;i<=q;i++) {
door[i].a = cin.nextInt();
door[i].b = cin.nextInt();
door[i].c = cin.nextInt();
door[i].d = cin.nextInt();
to[door[i].a][door[i].b] = i;
}
ex = cin.nextInt(); ey = cin.nextInt();
int ans = bfs();
if(ans==-1) System.out.println("No solution");
else System.out.println(ans);
}
}