Light OJ Basic Math
2013-03-03 13:46
337 查看
题目:Basic
Math
一共19道题
其中8道:1008 1010 1116 1214 1214 1311 1354 1414 已经在Beginners
Problems做过了,详见Light
OJ Beginners Problems
剩下的11道,除却A
Question of Time这道之外,另外10道已解决:
1020 A Childhood
Game
简单博弈
1078 Integer
Divisibility
1148 Mad
Counting
考察map
1179 Josephus
Problem
约瑟夫经典问题
1275 Internet
Service Providers
1297 Largest
Box
1323 Billiard
Balls
1349 Aladdin
and the Optimal Invitation
1369 Answering
Queries
1410 Consistent
Verdicts
A Question
of Time
不会
Math
一共19道题
其中8道:1008 1010 1116 1214 1214 1311 1354 1414 已经在Beginners
Problems做过了,详见Light
OJ Beginners Problems
剩下的11道,除却A
Question of Time这道之外,另外10道已解决:
1020 A Childhood
Game
简单博弈
// 博弈 每次和对手拿的石子和为3 // 对3取余之外的再讨论 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int main() { int t; scanf("%d",&t); for(int cases=1;cases<=t;cases++) { string s; int n; scanf("%d",&n); cin>>s; int tag; if(s=="Alice") tag=1; else tag=0; printf("Case %d: ",cases); if(tag) // last lose Alice first { n--; if(n%3==0) printf("Bob\n"); else printf("Alice\n"); } else // last win Bob first { if(n%3==0) printf("Alice\n"); else printf("Bob\n"); } } return 0; }
1078 Integer
Divisibility
// 只需要记录模n范围内能否整除 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { int t; scanf("%d",&t); for(int i=1;i<=t;i++) { int n,a,ans=0,cnt=1; scanf("%d%d",&n,&a); ans+=a; while(ans%n!=0) { cnt++; ans*=10; ans+=a; ans%=n; } printf("Case %d: %d\n",i,cnt); } return 0; } // 开始的时候sb了,写了这么个鬼代码TLE /* #include <cstring> #include <vector> #include <iostream> #include <algorithm> #include <cstdio> #include <string> using namespace std; bool is_ok(vector<long long>a,int b) { for(int i=a.size()-1;i>=0;i--) { long long cnt=(a[i]%b)*10; if(cnt!=0&&i==0) return false; if(i!=0) a[i-1]+=cnt; } return true; } void solve() { int b,num; scanf("%d%d",&b,&num); vector<long long>a; a.push_back(num); while(!is_ok(a,b)) a.push_back(num); printf("%d\n",a.size()); } int main() { int t; scanf("%d",&t); for(int ii=1;ii<=t;ii++) { printf("Case %d: ",ii); solve(); } return 0; } */
1148 Mad
Counting
考察map
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <map> using namespace std; map<int,int>m; int main() { int t,a; scanf("%d",&t); for(int i=1;i<=t;i++) { int n; scanf("%d",&n); m.clear(); for(int j=1;j<=n;j++) { scanf("%d",&a); m[a]++; } map<int,int>::iterator it; int ans=0; for(it=m.begin();it!=m.end();it++) { ans+=(it->first+it->second)/(it->first+1)*(it->first+1); } printf("Case %d: %d\n",i,ans); } return 0; }
1179 Josephus
Problem
约瑟夫经典问题
#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> using namespace std; int josephus(int n,int m) { if(n==2) return (m%2)?2:1; int v=(m+josephus(n-1,m))%n; if(v==0) v=n; return v; } int solve(int n,int m) { if(m==1) return n; if(n==1) return 1; if(m>=n) return josephus(n,m); int l=(n/m)*m; int j=josephus((n-(n/m)),m); if(j<=n-1) return l+j; j-=n-1; int t=(j/(m-1))*m; if((j%(m-1))==0) return t-1; return t+(j%(m-1)); } int main() { int t; scanf("%d",&t); for(int i=1;i<=t;i++) { int m,n; scanf("%d%d",&n,&m); printf("Case %d: %d\n",i,solve(n,m)); } return 0; }
1275 Internet
Service Providers
#include <cstdio> #include <algorithm> #include <iostream> using namespace std; int main() { int t; scanf("%d",&t); for(int i=1;i<=t;i++) { int n,c; scanf("%d%d",&n,&c); printf("Case %d: ",i); // t*c-n*t^2 // n=0 t=1; // -n*(t^2-(c/n)*t+...)+... if(n==0) printf("0\n"); else { int a=c/(2*n); if(a*2*n<=c) { int b=a+1; if(b*c-n*b*b>a*c-n*a*a) a=b; } else { int b=a-1; if(b*c-n*b*b>a*c-n*a*a) a=b; } printf("%d\n",a); } } return 0; }
1297 Largest
Box
#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int main() { int t; scanf("%d",&t); for(int i=1;i<=t;i++) { double w,l; scanf("%lf%lf",&w,&l); double x=w*w+l*l-w*l; x=sqrt(x); double y=w+l; double ans=(y-x)/6.0; ans=(w-2*ans)*(l-2*ans)*ans; printf("Case %d: %lf\n",i,ans); } return 0; }
1323 Billiard
Balls
//根据题图提示,每次碰撞之后可以看做是两球或多球互相交换 //实质上最终球的坐标与交换过程无关,仅考虑1000个球各自与桌壁的碰撞即可 #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int l,w,n,tt; struct dir { int x,y; }; dir d[8]; struct Ball { dir dd; int x,y; }; Ball ball[1001]; void init_direction() { d[0].x=1,d[0].y=0; d[1].x=1,d[1].y=1; d[2].x=0,d[2].y=1; d[3].x=-1,d[3].y=1; d[4].x=-1,d[4].y=0; d[5].x=-1,d[5].y=-1; d[6].x=0,d[6].y=-1; d[7].x=1,d[7].y=-1; } void bounce(Ball &bb) { int dx,dy; dx=tt%(2*l); dy=tt%(2*w); if(bb.dd.x==1&&bb.dd.y==1)// NE { if(dx<l-bb.x) bb.x+=dx; else if(dx<2*l-bb.x) { bb.x+=dx; bb.x=2*l-bb.x; } else bb.x=dx-(2*l-bb.x); if(dy<w-bb.y) bb.y+=dy; else if(dy<2*w-bb.y) { bb.y+=dy; bb.y=2*w-bb.y; } else bb.y=dy-(2*w-bb.y); } else if(bb.dd.x==1&&bb.dd.y==-1) // SE { if(dx<l-bb.x) bb.x+=dx; else if(dx<2*l-bb.x) { bb.x+=dx; bb.x=2*l-bb.x; } else bb.x=dx-(2*l-bb.x); if(dy<w+bb.y) { bb.y-=dy; if(bb.y<0) bb.y=bb.y*(-1); } else bb.y=w-(dy-(bb.y+w)); } else if(bb.dd.x==-1&&bb.dd.y==1)// NW { if(dx<bb.x) bb.x-=dx; else if(dx<bb.x+l) { bb.x-=dx; bb.x*=(-1); } else { dx-=(bb.x+l); bb.x=l-dx; } if(dy<w-bb.y) bb.y+=dy; else if(dy<2*w-bb.y) { bb.y+=dy; bb.y=2*w-bb.y; } else bb.y=dy-(2*w-bb.y); } else if(bb.dd.x==-1&&bb.dd.y==-1) { if(bb.x>dx) bb.x-=dx; else if(dx<bb.x+l) { bb.x-=dx; bb.x*=(-1); } else { dx-=(bb.x+l); bb.x=l-dx; } if(dy<w+bb.y) { bb.y-=dy; if(bb.y<0) bb.y=bb.y*(-1); } else bb.y=w-(dy-(bb.y+w)); } } bool cmp(Ball a,Ball b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } void solve() { scanf("%d%d%d%d",&l,&w,&n,&tt); string str; for(int i=1;i<=n;i++) { scanf("%d%d",&ball[i].x,&ball[i].y); cin>>str; if(str=="NW") ball[i].dd=d[3]; else if(str=="NE") ball[i].dd=d[1]; else if(str=="SE") ball[i].dd=d[7]; else if(str=="SW") ball[i].dd=d[5]; bounce(ball[i]); } } int main() { int t; scanf("%d",&t); init_direction(); for(int i=1;i<=t;i++) { solve(); printf("Case %d:\n",i); sort(ball+1,ball+n+1,cmp); for(int ii=1;ii<=n;ii++) { printf("%d %d\n",ball[ii].x,ball[ii].y); } } return 0; }
1349 Aladdin
and the Optimal Invitation
// http://lbv-pc.blogspot.com/2012/07/aladdin-and-optimal-invitation.html #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <map> #include <cmath> using namespace std; map<int,int>mx,my; int dx,dy; int m,n,q; void solve() { mx.clear(); my.clear(); scanf("%d%d%d",&m,&n,&q); __int64 total=0;//5w*1w可能会超 for(int n_a=1;n_a<=q;n_a++) { int ii,jj,kk; scanf("%d%d%d",&ii,&jj,&kk); total+=kk; mx[ii]+=kk; my[jj]+=kk; } __int64 tmp=0; map<int,int>::iterator it=mx.begin(); for(;it!=mx.end();it++) { dx=it->first; tmp+=it->second; if(tmp>=(total+1)/2) break; } tmp=0; it=my.begin(); for(;it!=my.end();it++) { dy=it->first; tmp+=it->second; if(tmp>=(total+1)/2) break; } } int main() { int Cases; scanf("%d",&Cases); for(int cases=1;cases<=Cases;cases++) { solve(); printf("Case %d: %d %d\n",cases,dx,dy); } return 0; } /* 胖子的代码 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; __int64 x[50050],y[50050]; int main() { int m,n,w,a,b; __int64 po; int i,j,k,t,p; cin>>t; for (i=1;i<=t;i++) { memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); cin>>m>>n>>w; po=0; while (w--) { cin>>a>>b>>p; x[a]+=p; y[b]+=p; po+=p; } __int64 sum=0; j=0; while (sum<(po+1)/2) { j++; sum+=x[j]; } cout<<"Case "<<i<<": "<<j<<" "; sum=0; j=0; while (sum<(po+1)/2) { j++; sum+=y[j]; } cout<<j<<endl; } } */
1369 Answering
Queries
// 我会说这题让我WA死的原因是因为我的__int64的输入输出是%I64d么 // 改成 %lld 就过了 坑死我了 // 直接用他给的公式当然会超时,转换成系数固定的方程 // 每次修改的时候更改ans,查询的时候直接输出ans #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstring> using namespace std; const int maxn=100005; __int64 a[maxn]; __int64 ans=0; int n,t,q; __int64 operation() { int tmp=n-1; __int64 sum=0; for(int i=1;i<=n;i++) { if(i>n+1-i) break; else if(i==n+1-i) { sum+=tmp*a[i]; } else sum+=tmp*(a[i]-a[n+1-i]); tmp-=2; } return sum; } void Query() { printf("%lld\n",ans); } void change(__int64 u,__int64 v) { u++; if(u>n) return ; ans+=(n-1-(u-1)*2)*(v-a[u]); a[u]=v; return; } void solve() { scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); ans=operation(); for(int i=1;i<=q;i++) { int tag; scanf("%d",&tag); if(tag==1) Query(); else { __int64 x,y; scanf("%lld%lld",&x,&y); change(x,y); } } } int main() { scanf("%d",&t); for(int cases=1;cases<=t;cases++) { printf("Case %d:\n",cases); solve(); } return 0; }
1410 Consistent
Verdicts
#include <cstdio> #include <algorithm> #include <map> #include <vector> #include <iostream> using namespace std; struct point { int x,y; }; point p[1010]; vector<__int64>v; int n,cases; __int64 dis(point a,point b) { return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); } void solve() { v.clear(); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&p[i].x,&p[i].y); for(int j=0;j<i;j++) v.push_back(dis(p[i],p[j])); } sort(v.begin(),v.end()); vector<__int64>::iterator it=unique(v.begin(),v.end()); printf("Case %d: %d\n",cases,it-v.begin()+1); } int main() { int t; scanf("%d",&t); for(cases=1;cases<=t;cases++) { solve(); } return 0; }
A Question
of Time
不会
相关文章推荐
- Light OJ Basic Geometry
- 【light-oj】-1331 - Agent J(几何,水)
- light--oj--1116--Ekka Dokka(数学问题)
- Light OJ Aladdin and the Flying Carpet(约数个数)
- light oj A Dangerous Maze(概率dp求期望)
- lightoj Basic Math 数论基础题 1414+1010+1020+1078+1116+1148+1179+1214+1275+1294+1297+1311+1323+1349+1354
- 【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
- 【light-oj】-1006 - Hex-a-bonacci(思维)
- light--oj--1294-- Positive Negative Sign(数学规律)
- Dominik Göddeke -- GPGPU::Basic Math Tutorial
- B - Discovering Gold Light oj 1030
- Light-oj-1045 lightoj1045 - Digits of Factorial (N!不同进制的位数)
- 【light-oj】-1109 - False Ordering(数学)
- Basic 2D Math(zz)
- 【Light-oj】-1043 - Triangle Partitioning(数学)
- 【light-oj】-’1307 - Counting Triangles(二分)
- Light OJ Dynamic Programming
- 蓝桥杯 OJ BASIC-1 闰年判断
- light--oj--1214-- Large Division
- 【Light-oj】-1414 - February 29(容斥原理)