zoj3836 Circulation pipe

Circulation pipe

Time Limit: 4 Seconds       Memory Limit: 65536 KB

Darkgy is a transport pipe master. One day, due to some strange redstone signal, an Iron pipe changed its direction and make a part of the pipe system become a circulation pipe.

The circulation pipe consists of L unit pipe numbered from 0 to L-1. Every K ticks, an item will input into pipe 0, and it will be transported in pipes with 1 unit pipe length per tick speed from pipe 0 to pipe L-1. When it was transported into pipeL-1, its direction will reversed and will be transported from L-1 to 0. When it reached pipe 0, its direction will be reversed again.

This process will repeat until the moment when there are more than C items in one of the L pipes, C is the capacity of each pipe.

For example, if L=5, K=3, C=1.

In tick 0, the first item will input into pipe 0.

In tick 3, it will be transported into pipe 3 and the second item will input into pipe 0.

In tick 4, the first item reached pipe 4 and its direction reversed, at the same time, the second item moved into pipe 1.

In tick 6, the third item appeared in pipe 0, the first item moved into pipe 2 while the second item was in pipe 3. Though the first item and the second item crossed, but......it does not matter XD.

In tick 7, the first item and the third item meet in pipe 1, and pipe 1 blast.

Darkgy want to know in which tick, the circulation pipe will blast.

Input

There are large amount of test cases, for each test case, there will be only one line with 3 integers 1 ≤ LKC ≤ 104which was mentioned in the description.

Output

For each test case, you should output only one line with an integer T which means the tick when circulation pipe was blast.

Sample Input
5 3 1
1 1 1
1 1 2
1 1 3
Sample Output
7
1
2
3
 
这题好难!首先可以肯定的是:第一次满足题意的一定是第一个小球,因为后面的小球是重复前面小球的运动过程的。那么枚举第一个小球在[0,L-1]的某个位置,并且枚举正反两个方向,注意在位置0和L-1处是没有正反之分的,要特殊处理。
当第一个小球确定后,我们可以递推后面的所有小球,当然不能暴力,应该解方程。在位置i,如果与第一个小球同方向,那么显然距离差是t*2*(L-1),2*(L-1)在此题就是小球的运动周期,只有当t*2*(L-1)%K=0时,这样的t才符合要求,因为小球间隔距离是K的倍数。当然在i的位置小球可以与第一个小球反向,显然如果存在的话(当然不一定存在),那么一定是夹在两个同向球之间的,否则连着两个同向的,那么后面重复前面过程,显然在i位置都得是同向的了。
如何计算反向球呢?这要分情况讨论。
第一个球正向:
那反向球应该从i到0再到i,加上周期,即(t*2*(L-1)+2*i)%K=0;
第一个球反向:
那么正向球应该从i到L-1再到i,加上周期,即(t*2*(L-1)+2*(L-1-i))%K=0;

这些方程可用扩展欧几里得来解,当然可能无解。

然后反推一直到最后一个在i位置出现的小球(第C+1个),这些时间可以计算得到,然后加上从位置0到i位置最后一个小球(小球有方向)的移动步数就是该种情况下的解。

还有注意L=1的时候很特殊,必须要特判。

代码公式有点多,跑了2秒!这题应该有更简单解法,不过目前还没想到。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;

ll a,b,x,y,q;
ll l,k,c;
ll myabs(ll x){
    return x>0?x:-x;
}
void extend_gcd(ll a,ll b,ll &x,ll &y,ll &q){
    if(b==0){x=1;y=0;q=a;}
    else{
        extend_gcd(b,a%b,x,y,q);
        ll tmp=x;x=y;y=tmp-a/b*y;
    }
}
ll forward(int i){
    ll ans=0;
    a=2*(l-1),b=-k;
    extend_gcd(a,b,x,y,q);
    ll t=k/myabs(q)*a;
    if(i==0||i==l-1) return (c-1)*t+i;
    if(2*i%q) ans=(c-1)*t+i;
    else{
        x*=-2*i/q;
        ll tmp=x/(k/q),res;
        res=x-k/q*tmp;
        if(res<0) res+=myabs(k/q);
        if(c&1) ans=c/2*t+i;
        else ans=(c-1)/2*t+res*a+2*i+a-i;
    }
    return ans;
}
ll backward(int i){
    ll ans=0;
    a=2*(l-1),b=-k;
    extend_gcd(a,b,x,y,q);
    ll t=k/myabs(q)*a;
    if(i==0||i==l-1) return (c-1)*t+i;
    if(2*(l-i-1)%q) ans=(c-1)*t+a-i;
    else{
        x*=-2*(l-i-1)/q;
        ll tmp=x/(k/q),res;
        res=x-k/q*tmp;
        if(res<0) res+=myabs(k/q);
        if(c&1) ans=c/2*t+a-i;
        else ans=(c-1)/2*t+res*a+2*(l-i-1)+i;
    }
    return ans;
}
int main()
{
    ll res;
    while(~scanf("%lld%lld%lld",&l,&k,&c)){
        if(l==1) {printf("%lld\n",c*k);continue;}
        c+=1;
        res=min(forward(0),backward(l-1));
        for(int i=1;i<l-1;i++){
            res=min(res,forward(i));
            res=min(res,backward(i));
        }
        printf("%lld\n",res);
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值