lightoj Basic Math 数论基础

这里是除去Beginners Problems后的部分

1020 – A Childhood Game

巴什博奕(Bash Game)

#include<bits/stdc++.h>using namespace std;int main(void){int t,Case=0;int n;char s[10];scanf("%d",&t);while(t–){scanf("%d%s",&n,&s);printf("Case %d: ",++Case);if(strcmp(s,"Alice")==0){if(n%3==1)puts("Bob");elseputs("Alice");}else{if(n%3==0)puts("Alice");elseputs("Bob");}}return 0;}

1078 – Integer Divisibility#include<bits/stdc++.h>using namespace std;int main(void){int t,Case=0;int n,digit;scanf("%d",&t);while(t–){scanf("%d%d",&n,&digit);int cnt=1;int tmp=digit;while(tmp%n!=0){tmp=(tmp*10+digit)%n;cnt++;}printf("Case %d: %d\n",++Case,cnt);}return 0;}

1148 – Mad Counting#include<bits/stdc++.h>using namespace std;map<int,int> ma;map<int,int>::iterator ite;int main(void){int t,Case=0;int n,x;scanf("%d",&t);while(t–){ma.clear();scanf("%d",&n);while(n–){scanf("%d",&x);ma[x]++;}int ans=0;for(ite=ma.begin(); ite!=ma.end(); ite++){ans+=(ite->second+ite->first)/(ite->first+1)*(ite->first+1);}printf("Case %d: %d\n",++Case,ans);}return 0;}1179 – Josephus Problem

约瑟夫环

#include<bits/stdc++.h>using namespace std;int main(void){int t,Case=0;int n,k;scanf("%d",&t);while(t–){scanf("%d%d",&n,&k);int ans=0;for(int i=2;i<=n;i++){ans=(ans+k%i)%i;}printf("Case %d: %d\n",++Case,ans+1);}return 0;}1275 – Internet Service Providers

方程极值

#include<bits/stdc++.h>using namespace std;const double eps=1e-6;int main(void){int t,Case=0;double n,c;scanf("%d",&t);while(t–){scanf("%lf%lf",&n,&c);printf("Case %d: %d\n",++Case,n>eps?(int)(c/2/n+0.5-eps):0);}return 0;}1297 – Largest Box

方程极值

#include<bits/stdc++.h>using namespace std;double maxx(double w,double l){double a=12;double b=-4*(w+l);double c=w*l;double x=-b-sqrt(b*b-4*a*c);x/=2*a;return x;}double fun(double w,double l){double x=maxx(w,l);return (w-2*x)*(l-2*x)*x;}int main(void){int t,Case=0;double w,l;scanf("%d",&t);while(t–){scanf("%lf%lf",&w,&l);printf("Case %d: %f\n",++Case,fun(w,l));}return 0;}1323 – Billiard Balls

碰撞可以为穿透,那么可以无视别的球直接求最终位置

#include<bits/stdc++.h>using namespace std;struct node{int x;int y;int dx;int dy;} p[1010];int cmp(const node a,const node b){if(a.x==b.x)return a.y<b.y;return a.x<b.x;}int main(void){int t,Case=0;int l,w,n,k;scanf("%d",&t);while(t–){scanf("%d%d%d%d",&l,&w,&n,&k);char s[5];for(int i=0; i<n; i++){scanf("%d%d%s",&p[i].x,&p[i].y,s);if(s[0]=='N')p[i].dy=1;elsep[i].dy=-1;if(s[1]=='W')p[i].dx=-1;elsep[i].dx=1;}for(int i=0; i<n; i++){p[i].x+=p[i].dx*k;p[i].y+=p[i].dy*k;while(!((p[i].x>=0)&&(p[i].x<=l)&&(p[i].y>=0)&&p[i].y<=w)){if(p[i].x<0)p[i].x=-p[i].x;else if(p[i].x>l)p[i].x=2*l-p[i].x;if(p[i].y<0)p[i].y=-p[i].y;else if(p[i].y>w)p[i].y=2*w-p[i].y;}}sort(p,p+n,cmp);printf("Case %d:\n",++Case);for(int i=0; i<n; i++)printf("%d %d\n",p[i].x,p[i].y);}return 0;}1349 – Aladdin and the Optimal Invitation

求中位数,每个点的人数即访问次数

#include<bits/stdc++.h>using namespace std;struct node{int x;int y;int z;} p[50050];int cmp1(const node a,const node b){return a.x<b.x;}int cmp2(const node a,const node b){return a.y<b.y;}int main(void){int t,Case=0;int m,n,q;int x,y,z;scanf("%d",&t);while(t–){scanf("%d%d%d",&m,&n,&q);int sum=0;for(int i=0; i<q; i++){scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);sum+=p[i].z;}sum=(sum+1)/2;sort(p,p+q,cmp1);z=0;for(int i=0; i<q; i++){z+=p[i].z;if(z>=sum){x=p[i].x;break;}}sort(p,p+q,cmp2);z=0;for(int i=0; i<q; i++){z+=p[i].z;if(z>=sum){y=p[i].y;break;}}printf("Case %d: %d %d\n",++Case,x,y);}return 0;}1369 – Answering Queries

理解代码块并优化

#include<bits/stdc++.h>using namespace std;long long a[100010];int main(void){int t,Case=0;int n,q;scanf("%d",&t);while(t–){scanf("%d%d",&n,&q);for(int i=0; i<n; i++)scanf("%lld",&a[i]);long long sum=0;for(int i=0; i<n; i++)sum+=a[i]*(n-1-2*i);printf("Case %d:\n",++Case);int index;long long x,y;while(q–){scanf("%d",&index);if(index==1)printf("%lld\n",sum);else{scanf("%lld%lld",&x,&y);sum-=a[x]*(n-1-2*x);a[x]=y;sum+=a[x]*(n-1-2*x);}}}return 0;}1410 – Consistent Verdicts如果你在以的话,别人就会知道你害怕被说,他们就会加倍地说你,

lightoj Basic Math 数论基础

相关文章:

你感兴趣的文章:

标签云: