ZOJ Problem Set - 1505 Solitaire
飞机票:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=505
直接进行BFS就行,需要注意的是状态转移,对四个点进行排序,然后按100,10000,1000000的系数来判断是否访问过(剪枝),因为一个点可以上下左右跳一格或两格如果一格可以则不进行两格(剪枝)//
// main.cpp // zoj1505 // // Created by cfhaiteeh on 12/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <map> #include <algorithm> using namespace std; struct he{ int ai[9]; int val; int t; }; int bi[9]; int ci[9]; map<int,int> _M; int _map[9][9]; int rn[9]; int cn[9]; int toans; bool getdir(int r,int c){ if(r<1||r>8)return 0; if(c<1||c>8)return 0; if(_map[r][c]==1)return 0; return 1; } int cm[5]; int cr[]={-1,-2,1,2,0,0,0,0}; int cc[]={0,0,0,0,-1,-2,1,2}; int getcm(){ sort(cm+1,cm+1+4); return cm[1]*1000000+cm[2]*10000+cm[3]*100+cm[4];; } bool BFS(){ queue<he> Q; he ne; for(int i=1;i<=8;i++)ne.ai[i]=bi[i]; for(int k=2;k<=8;k+=2){ cm[k/2]=ne.ai[k-1]*10+ne.ai[k]; } int ct=getcm(); _M[ct]=1; ne.val=8; ne.t=ct; Q.push(ne); while(!Q.empty()){ he c=Q.front(); Q.pop(); if(c.t==toans)return 1; if(c.val==0)continue; memset(_map,0,sizeof(_map)); for(int i=1;i<=8;i+=2){ int row=c.ai[i]; int col=c.ai[i+1]; _map[row][col]=1; } for(int i=1;i<=8;i+=2){ int row=c.ai[i]; int col=c.ai[i+1]; int st=0; for(int j=0;j<8;j++){ if(getdir(row+cr[j],col+cc[j])){ rn[++st]=row+cr[j]; cn[st]=col+cc[j]; if(j%2==0) { j++; } } } for(int j=1;j<=st;j++){ he nex; for(int k=1;k<=8;k++){ nex.ai[k]=c.ai[k]; } nex.ai[i]=rn[j]; nex.ai[i+1]=cn[j]; nex.val=c.val-1; for(int k=2;k<=8;k+=2){ cm[k/2]=nex.ai[k-1]*10+nex.ai[k]; } int ct=getcm(); if(_M[ct]==1)continue; nex.t=ct; _M[ct]=1; Q.push(nex); } } } return 0; } void init(){ _M.clear(); for(int i=2;i<=8;i++)scanf("%d",&bi[i]); for(int i=1;i<=8;i++)scanf("%d",&ci[i]); for(int k=2;k<=8;k+=2){ cm[k/2]=ci[k-1]*10+ci[k]; } int ct=getcm(); toans=ct; } int ans; void play(){ ans=BFS(); } void pri(){ if(ans)printf("YES\n"); else printf("NO\n"); } int main() { // insert code here... while(scanf("%d",&bi[1])!=EOF){ init(); play(); pri(); } return 0; }
ZOJ Problem Set - 2371 Three powers
飞机票:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1347
易知对于3^i次方 3^0+3^1+....3^(i-1)<3^i 且对于k项,一共有2^k-1(不包括空集,对空集进行特殊处理) 所以当n==2^k-1时,则前k项都包括,当n>2^k-1&&n<2^(k+1)-1时,包含第k+1项,将n-(2^k-1)后就是k+1项的事情了,领n1=n-(2^k-1),
n2=n1-1时,此时k+1已经处理了,则n2又变为原来的n,进行递归求解。
import java.math.BigInteger; import java.util.*; public class Maintf { public static BigInteger fac[]; public static ArrayList<Integer> al; public static void DFS(BigInteger x){ if(x.compareTo(BigInteger.ZERO)==0)return; for(int i=1;i<64;i++){ if(x.compareTo(fac[i])==0){ for(int j=i;j>=1;j--){ al.add(j); } return; } if(x.compareTo(fac[i])>0&&x.compareTo(fac[i+1])<0){ x=x.subtract(fac[i]).subtract(BigInteger.ONE); al.add(i+1); DFS(x); return; } } } public static void main(String[] args) { // TODO Auto-generated method stub fac=new BigInteger[123]; fac[1]=BigInteger.ONE; BigInteger two=new BigInteger("2"); for(int i=2;i<=64;i++){ fac[i]=fac[i-1].multiply(two).add(BigInteger.ONE); } BigInteger pow3[]=new BigInteger[123]; pow3[1]=BigInteger.ONE; BigInteger three=new BigInteger("3"); for(int i=2;i<=64;i++){ pow3[i]=pow3[i-1].multiply(three); } al=new ArrayList<>(); Scanner sca=new Scanner(System.in); while(sca.hasNext()){ BigInteger n=sca.nextBigInteger(); if(n.compareTo(BigInteger.ZERO)==0)break; if(n.compareTo(BigInteger.ONE)==0){ System.out.println("{ }"); continue; } al.clear(); n=n.subtract(BigInteger.ONE); DFS(n); System.out.print("{"); for(int i=al.size()-1;i>=0;i--){ System.out.print(" "+pow3[al.get(i)]); if(i!=0)System.out.print(","); else System.out.print(" "); } System.out.println("}"); } } }
AtCoder Beginner Contest 090
A - Diagonal String
水题:记录直接输出就好
// // main.cpp // atcoder // // Created by cfhaiteeh on 11/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstring> #include <cmath> #include <algorithm> using namespace std; char ch[4][4]; int main() { for(int i=1;i<=3;i++)scanf("%s",ch[i]+1); for(int i=1;i<=3;i++)printf("%c",ch[i][i]); printf("\n"); return 0; }
B - Palindromic Numbers
数据较小,判断记录总的多少就好
// // main.cpp // atcoder // // Created by cfhaiteeh on 11/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstring> #include <cmath> #include <algorithm> using namespace std; char ch[4][4]; int ai[123456]; int ci[6]; int main() { for(int i=10000;i<=99999;i++){ int st=0; int t=i; while(t>0){ int p=t%10; t=t/10; ci[++st]=p; } bool flag=1; for(int j=1;j<=2;j++){ if(ci[j]==ci[5-j+1]){ continue; } flag=0; break; } if(flag)ai[i]=1; } for(int i=10000;i<=99999;i++)ai[i]+=ai[i-1]; int l,r; while(scanf("%d%d",&l,&r)!=EOF){ printf("%d\n",ai[r]-ai[l-1]); } return 0; }
C - Flip,Flip, and Flip......
找规律,特判就行
// // main.cpp // atcoder // // Created by cfhaiteeh on 11/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #define LL long long using namespace std; LL n,m; LL ans; int main() { while(scanf("%lld%lld",&n,&m)!=EOF){ if(n>m)swap(n,m); if(n==1&&m==1)printf("1\n"); else { if(n==1)printf("%lld\n",m-2); else { LL ans=(n-2)*(m-2); printf("%lld\n",ans); } } } return 0; }
D - Remainder Reminder
对于除数进行枚举,易知当除数为i时,1~n的余数为1,2,。。。。0,1,2.。。。0 循环。所以只要直接判断n里有多少组1.2.。。0即可,对于不能整除时进行特判就行。复杂度为O(n)本人对0也进行了特判,主要感觉0怪怪的。。。//
// main.cpp // atcoder // // Created by cfhaiteeh on 11/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #define LL long long using namespace std; LL n,m; LL ans; int main() { while(scanf("%lld%lld",&n,&m)!=EOF){ ans=0; if(m==0){ ans=n; for(int i=2;i<=n;i++){ LL p=n/i; ans+=p; } m=1; } for(LL i=m+1;i<=n;i++){ LL p=n/i; LL q=(n%i)-m+1; if(q<0)q=0; LL z=i-m; ans=ans+z*(p)+q; } printf("%lld\n",ans); } return 0; }
ZOJ Problem Set - 2273 Number Sequence III
飞机票:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1273
对于每个回合,求出每个回合第一个数字是多少,则第i回合增量为2^i的等差数列。然后对于位置进行处理,算出当前数字的位置中最大能到达的层数。该层数和前一个数字的层数比较,选出最大的即为答案
// // main.cpp // zoj2273 // // Created by cfhaiteeh on 11/03/2018. // Copyright © 2018 cfhaiteeh. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define LL long long using namespace std; int turn[5*123456]; bool vis[5*123456]; int ans[100000]; int tpow[5*123456]; int wzz[100000]; void getTurn(){ int c=1; int now=1; int t=2; int before=1; int z=0; while(true){ now=now^1; if(now==0){ int st=c+before; for(int i=st;i<=500000;i+=t){ vis[i]=1; } tpow[++z]=t; t*=2; turn[z]=st; }else{ for(int i=c;i<=500000;i+=t){ vis[i]=1; } before=t; tpow[++z]=t; t*=2; bool flag=1; turn[z]=c; for(int i=c;i<=500000;i++){ if(!vis[i]){ c=i; flag=0; break; } } if(flag)break; } } } void getAw(){ ans[1]=1; ans[2]=1; ans[3]=3; wzz[1]=1; wzz[2]=1; wzz[3]=3; int s=3; int ci[6]; for(int i=4;i<=99999;i++){ int tp=i; int op=0; int now=-1; int ip=ans[i-1]; int ww=wzz[i-1]; for(int k=1;k<=500000;k++){ int a=wzz[i-1]-turn[k]; if(a<0)continue; if(a%tpow[k]==0){ now=k; break; } } while(tp>0){ int c=tp%10; ci[++op]=c; tp=tp/10; } for(int j=op;j>=1;j--){ s++; int wz=s; for(int k=1;k<=500000;k++){ int a=wz-turn[k]; if(a<0)continue; if(a%tpow[k]==0){ if(k>now){ now=k; ip=ci[j]; ww=wz; } break; } } } ans[i]=ip; wzz[i]=ww; } } int main() { getTurn(); getAw(); // cout<<"end"<<endl; int a; while(scanf("%d",&a)!=EOF){ printf("%d\n",ans[a]); } return 0; }
现在起,对于复杂点的题目写题解
人生就是兜兜转转,请相信自己每次的决定
ZOJ Problem Set - 2974 Just Pour the Water
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> #include <stack> #include <queue> #include <vector> #include <set> using namespace std; const int maxn=23; double ai[maxn]; struct matrix { double e[maxn][maxn]; }; matrix Cal(matrix a,matrix b,int len) { matrix c; for(int i=1; i<=len; i++) { for(int j=1; j<=len; j++) { c.e[i][j]=0; for(int k=1; k<=len; k++) { c.e[i][j]+=a.e[i][k]*b.e[k][j]; } } } return c; } matrix a,s; int n; void exp_pow(int b) { while(b) { if(b&1)s=Cal(s,a,n); a=Cal(a,a,n); b>>=1; } } void init() { scanf("%d",&n); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { s.e[i][j]=0; } } for(int i=1; i<=n; i++) { scanf("%lf",&s.e[1][i]); } int k,t; for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { a.e[i][j]=0; } } for(int i=1; i<=n; i++) { scanf("%d",&k); if(k==0) { a.e[i][i]=1; } else { double fm=1.0/k; for(int j=1; j<=k; j++) { scanf("%d",&t); a.e[i][t]=fm; } } } } void play() { int k; scanf("%d",&k); exp_pow(k); bool first=1; for(int i=1; i<=n; i++) { if(!first)printf(" "); first=0; printf("%.2f",s.e[1][i]); } printf("\n"); } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3829 Known Notation
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #include <stack> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=1234; char ch[maxn]; int len; int ai[maxn]; int f; void init() { scanf("%s",ch+1); len=strlen(ch+1); f=0; for(int i=1; i<=len; i++) { if(ch[i]=='*')ai[i]=1; else ai[i]=0; f+=ai[i]; } } void play() { int f2=len-f; f2=f-f2; int ans=0; if(f2>=0) { ans=f2+1; } int before=ans; int st=len; for(int i=1; i<=len; i++) { if(ai[i]==1) { if(before<2) { while(ai[st]!=0) { st--; } ai[st]=1; before++; ans++; } else { before--; } } else { before++; } } if(ai[len]!=1&&f!=0)ans++; printf("%d\n",ans); } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3913 Bob wants to pour water
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const double PI=3.1415926535898; double CalSphere(double r,double h) { return PI*(r*h*h-1.0*h*h*h/3); } const int maxn=123456; struct he { double ch,w,l,h,r; } Sky[maxn*2]; double w,l,v; int n,m; double maxH; void init() { maxH=0; scanf("%lf%lf%lf%d%d",&w,&l,&v,&n,&m); for(int i=1; i<=n; i++) { scanf("%lf%lf%lf%lf",&Sky[i].ch,&Sky[i].w,&Sky[i].l,&Sky[i].h); Sky[i].ch=Sky[i].ch-Sky[i].h/2; maxH=max(maxH,Sky[i].ch+Sky[i].h); } for(int i=1; i<=m; i++) { scanf("%lf%lf",&Sky[n+i].ch,&Sky[n+i].r); Sky[n+i].ch=Sky[n+i].ch-Sky[n+i].r; maxH=max(maxH,Sky[n+i].ch+Sky[n+i].r*2); } } bool check(double h) { double nv=0; for(int i=1; i<=n; i++) { double mh=Sky[i].ch+Sky[i].h; if(mh<=h) { nv+=Sky[i].h*Sky[i].l*Sky[i].w; } else if(Sky[i].ch>=h) { } else { double hh=h-Sky[i].ch; nv+=hh*Sky[i].l*Sky[i].w; } } for(int i=1; i<=m; i++) { double mh=Sky[n+i].ch+Sky[n+i].r*2; if(mh<=h) { nv+=CalSphere(Sky[n+i].r,Sky[n+i].r*2); } else if(Sky[n+i].ch>=h) { } else { double hh=h-Sky[n+i].ch; nv+=CalSphere(Sky[n+i].r,hh); } } double cm=w*l*h; if(cm<=nv+v) { return 1; } else { return 0; } } double BRSearch() { double l=0; double r=maxH+1000; double ans=0; while(l<=r) { double mid=(l+r)/2; bool ck=check(mid); if(ck) { ans=mid; l=mid+EPS; } else { r=mid-EPS; } } return ans; } void play() { double ans=BRSearch(); if(ans>maxH) { for(int i=1; i<=n; i++) { v+=Sky[i].w*Sky[i].l*Sky[i].h; } for(int i=1; i<=m; i++) { v+=CalSphere(Sky[n+i].r,Sky[n+i].r*2); } ans=v/(w*l); printf("%.8f\n",ans); } else { printf("%.8f\n",ans); } } int T; int main() { scanf("%d",&T); while(T--) { init(); play(); } return 0; }
ZOJ Problem Set - 3908 Number Game
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <stack> #include <queue> #include <set> #define LL long long #define INF 1e9 #define EPS 1e-9 using namespace std; const int maxn=123456; int ai[maxn]; int Link[12345]; int bi[12345]; int T; int n,m,k; bool cmp(int a,int b) { return a>b; } void init() { scanf("%d%d%d",&n,&m,&k); memset(bi,0,sizeof(bi)); for(int i=1; i<=n; i++) { scanf("%d",&ai[i]); if(ai[i]==0)continue; bi[ai[i]]++; } Link[0]=0; bi[0]=100000000; for(int i=1; i<=10000; i++) { if(bi[i]==0)Link[i]=Link[i-1]; else Link[i]=i; } sort(ai+1,ai+1+n,cmp); } vector<int> V; int Find(int x) { if(Link[x]==x)return x; Link[x]=Find(Link[x]); return Link[x]; } void play() { V.clear(); for(int i=1; i<=n; i++) { int cz=k-ai[i]; if(ai[i]==0)continue; if(bi[ai[i]]==0)continue; bi[ai[i]]--; if(bi[ai[i]]==0)Link[ai[i]]=Link[ai[i]-1]; if(cz<=0)continue; if(cz>10000)cz=10000; int f=Find(cz); bi[f]--; if(bi[f]==0)Link[f]=Link[f-1]; V.push_back(f*ai[i]); } int p=V.size(); LL ans=0; sort(V.begin(),V.end(),cmp); for(int i=0; i<min(p,m); i++) { ans+=V[i]; } printf("%lld\n",ans); } int main() { scanf("%d",&T); while(T--) { init(); play(); } // cout << "Hello world!" << endl; return 0; }
ZOJ Problem Set - 3903 Ant
import java.math.BigInteger; import java.util.*; public class zoj3903 { public static BigInteger pow2(long p) { BigInteger big = BigInteger.ZERO; BigInteger nn = new BigInteger(p + ""); BigInteger nn1 = nn.add(BigInteger.ONE); BigInteger tw = new BigInteger(2 + ""); BigInteger si = new BigInteger(6 + ""); big = nn.multiply(nn1).multiply(nn.multiply(tw).add(BigInteger.ONE)); return big.divide(si); } public static BigInteger pow3(long p) { BigInteger big = BigInteger.ZERO; BigInteger nn = new BigInteger(p + ""); BigInteger nn1 = nn.add(BigInteger.ONE); BigInteger tw = new BigInteger(2 + ""); big = nn.multiply(nn1).divide(tw); return big.multiply(big); } public static void main(String[] args) { // TODO Auto-generated method stub int T; Scanner sca = new Scanner(System.in); T = sca.nextInt(); long p; BigInteger MOD = new BigInteger("1000000007"); long o = 1; BigInteger tw = new BigInteger(2 + ""); for (int i = 1; i <= T; i++) { p = Long.parseLong(sca.next()); p++; BigInteger q = new BigInteger((p - 1) + ""); BigInteger b1 = pow3(p).subtract(pow3(o)); BigInteger b2 = pow2(p).subtract(pow2(o)); BigInteger ans = b1.subtract(b2); BigInteger b3 = pow2((p - 1) * 2).subtract(pow2(p)); b3 = b3.multiply(q); BigInteger b4 = pow3((p - 1) * 2).subtract(pow3(p)); BigInteger b5 = pow2((p - 1) * 2).subtract(pow2(p)); b5=b5.multiply(new BigInteger(p+"")); BigInteger ans1 = b4.subtract(b5); ans1 = b3.subtract(ans1); ans = ans.add(ans1); ans =ans.add(pow2(p - 1).multiply(tw).multiply(tw)); ans = ans.divide(tw); BigInteger st = q.multiply(new BigInteger(p + "")).divide(tw) .multiply(q).multiply(q); ans = ans.add(st); ans = ans.mod(MOD); System.out.println(ans); } } }