UVa 10920 - Spiral Tap Solution

 #include <bits/stdc++.h>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    long long s,n;
    while(cin>>s>>n){
        if(s==0&&n==0)return 0;
        long long u=sqrt(n),x,y;
        /*0 indicates square of odd number , 1 indicates square of even number*/
        if(u&1){
            x=(u-1)/2,y=(u-1)/2;
            n-=u*u;
            if(n){
                y++;
                if(n<=u+1){
                    x-=n-1;
                }
                else{
                    n-=u+1;
                    x-=u;
                    y-=n;
                }
            }
        }
        else{
            if(u*u==n){
                x=-(u/2),y=-(u/2)+1;
            }
            else{
                x=-(u/2),y=-(u/2);
                n-=u*u+1;
                if(n){
                    // x++;
                    if(n<=u){
                        x+=n;
                    }
                    else{
                        n-=u;
                        x+=u;
                        y+=n;
                    }
                }
            }
        }
        x+=(s+1)/2,y+=(s+1)/2;

        cout<<"Line = "<<y<<", column = "<<x<<'.'<<endl;
    }
    return 0;
}

Comments