码迷,mamicode.com
首页 > 其他好文 > 详细

lightoj Basic Math 数论基础

时间:2015-05-19 22:46:30      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:basic math   lightoj   light oj   数论   

这里是除去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");
            else
                puts("Alice");
        }
        else
        {
            if(n%3==0)
                puts("Alice");
            else
                puts("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;
            else
                p[i].dy=-1;
            if(s[1]=='W')
                p[i].dx=-1;
            else
                p[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

问方案数,干脆直接统计有多少个不同的距离

#include<bits/stdc++.h>
using namespace std;

struct node
{
    int x;
    int y;
} p[701];
long long d[700*700+5];
int main(void)
{
    int t,Case=0;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int len=0;
        for(int i=0; i<n; i++)
        {
            scanf("%lld%lld",&p[i].x,&p[i].y);
            for(int j=0; j<i; j++)
                d[len++]=((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
        }
        sort(d,d+len);
        int ans=unique(d,d+len)-d;
        printf("Case %d: %d\n",++Case,ans+1);
    }
    return 0;
}

1430 - A Question of Time

这题一共交了20发,一共才24人AC,网上找都找不到题解,已刷吐

原本是用那个fun()直接求的,样例和自测数据都能过,交上去无限WA,所以改为二分(如果直接暴力复杂度是12*60*60*13*T==1e10会超时)

#include <bits/stdc++.h>
using namespace std;
//fun()=3600*(2*h0-hx)+60*(2*m0-13*mx)+2*s0
//13*sx=fun()
//13*sx=fun()+360*120
//13*sx=fun()-360*120
const double eps=1e-10;
const double eps2=1e-3;
int h0,m0,s0;
int t1,t2;
int ans[50];
int len;
double fun(int tmp)
{
    double h,m,s;
    s=tmp%(13*60)/13.0;
    tmp/=13*60;
    m=tmp%60;
    h=tmp/60;
    double ang0=(30*h0)+(m0/2.0)+(s0/120.0);
    double ang1=(30*h)+(m/2.0)+(s/120.0);
    double ang2=(6*m)+(s/10.0);
    return ang1+ang2-2*ang0;
}
int bs(int l,int r,double x)
{
    int mid;
    while(r-l>1)
    {
        mid=(l+r)/2;
        double cmp=fun(mid)-x;
        if(cmp>=eps)
            r=mid;
        else
            l=mid;
    }
    if(t1<=l&&l<=t2&&fabs(fun(l)-x)<eps2)
        return l;
    else if(t1<=r&&r<=t2&&fabs(fun(r)-x)<eps2)
        return r;
    return -1;
}
void solve(int i)
{
    int l=i*46800,r=l+46800-1;
    int tmp=bs(l,r,0.0);
    if(tmp!=-1)
    {
        ans[len++]=tmp;
    }
    tmp=bs(l,r,360.0);
    if(tmp!=-1)
    {
        ans[len++]=tmp;
    }
    tmp=bs(l,r,-360.0);
    if(tmp!=-1)
    {
        ans[len++]=tmp;
    }
}
int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    int t,Case=0;
    int h,m,s;
    scanf("%d",&t);
    while(t--)
    {
        len=0;
        scanf("%d:%d:%d",&h0,&m0,&s0);
        scanf("%d:%d:%d",&h,&m,&s);
        t1=((h*60+m)*60+s)*13;
        scanf("%d:%d:%d",&h,&m,&s);
        t2=((h*60+m)*60+s)*13;

        for(int i=0; i<12; i++)
        {
            solve(i);
        }

        sort(ans,ans+len);
        len=unique(ans,ans+len)-ans;
        printf("Case %d: %d\n",++Case,len);
        int mod;
        for(int i=0; i<len; i++)
        {
            mod=ans[i]%13;
            ans[i]/=13;
            s=ans[i]%60;
            ans[i]/=60;
            m=ans[i]%60;
            h=ans[i]/60;
            printf("%02d:%02d:%02d",h,m,s);
            if(mod)
                printf(" %d/13",mod);
            puts("");
        }
    }
}


lightoj Basic Math 数论基础

标签:basic math   lightoj   light oj   数论   

原文地址:http://blog.csdn.net/loolu5/article/details/45849519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!