位运算是计算机最基础的运算方式,其它运算都是基于此的。一定要好好掌握。
重剑无锋,大巧不工
#include <iostream> using namespace std; int main() { int n; while(cin>>n) cout<<n*(n+1)/2+1<<endl; return 0; }
#include <iostream> using namespace std; int n,x[100],y[100],s[100],p[100]; int v[100],l[100],map[100][100],ans; int find(int p) { for (int i=0; i<n; i++) if (map[p][i] && !v[i]) { v[i]=1; if (l[i]==-1 || find(l[i])) { l[i]=p; return 1; break; } } return 0; } int main() { int T; cin>>T; while (T--) { cin>>n; for (int i=0; i<n; i++) cin>>x[i]>>y[i]>>s[i]>>p[i]; for (int i=0; i<n; i++) for (int j=0; j<n; j++) { int way=(y[j]-y[i])*(y[j]-y[i])+(x[j]-x[i])*(x[j]-x[i]); if (way>=s[i]*s[i] && way<=p[i]*p[i]) map[i][j]=1; else map[i][j]=0; } ans=0; for (int i=0; i<n; i++) l[i]=-1; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) v[j]=0; if (find(i)) ans++; } cout<<ans<<endl; } }
#include <iostream> #include <cmath> using namespace std; int main() { int flag[33000],N=32768,p,q; for (int i=0; i<=N; i++) flag[i]=0; for (int i=2; i<=sqrt(N); i++) if (!flag[i]) { int j=i+i; while (j<=N) { flag[j]=1; j+=i; } } cin>>p; while (cin>>p) { q=0; if (p%2==0) for (int i=3; i<=p/2; i++) if (!flag[i] && !flag[p-i]) q++; cout<<q<<endl; } }
#include <iostream> using namespace std; const int way[4][2]={1,0,-1,0,0,1,0,-1}; int a[52][52],n,m,x,y; int day,castle,ability,v[52][52]; void visit(int x,int y) { for (int i=0; i<4; i++) { int xx=x+way[i][0],yy=y+way[i][1]; if (xx<1 || xx>n || yy<1 || yy>m) continue; if (v[xx][yy]==0) v[xx][yy]=1; } } int main() { int T; cin>>T; while (T--) { cin>>n>>m; for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) cin>>a[i][j]; cin>>x>>y; for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) v[i][j]=0; v[x][y]=2; castle=1; day=0; ability=a[x][y]; visit(x,y); while (castle<n*m) { day++; int flag=1; for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) if (v[i][j]==1 && ability>a[i][j]) { v[i][j]=3; castle++; flag=0; } for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) if (v[i][j]==3) { v[i][j]=2; ability++; visit(i,j); } if (flag) break; } if (castle==n*m) cout<<"YES "<<day<<endl; else cout<<"NO "<<castle<<endl; } }