Nefu 845-Finger Mountain (extended Euclid)

Source: Internet
Author: User

Mt. Wuzhishan
Description
The Sun Guo in the journey to the Great Heaven, Buddha ancestors came to yield him, said: "I play a game with you, if you have the ability, a loop out of my right hand, count you win, and then do not move Sword bitter war, please the Jade Emperor to live in the West, the Heavenly Palace let you, if you can not play the palm, you also the Nether for the demon, and then a few "The sage smells the words, the dark smile:" This tathagata very good stay! My old sun went to 108,000 miles in a loop. His palm, the radius of a foot, how can not jump out? "What do you think you can do," said the sharp-sounding way: " "Buddha Way:" Do it! Do it! "The right hand, but like a lotus leaf size." That Sage received the Ruyi, Cheer Divinity, will be a longitudinal, standing in the Buddha Palm, but the voice: I go out also! "You see him all the way to the clouds, the invisible went." The holy line, suddenly see there are five sards pillars, holding a green gas. He said: "This is the end of the road." This went back, the Tathagata testified, the spirit of the temple will be I sit also. "Flip the wings of the cloud, the path back to the place, standing in the palm of the Tathagata:" I have to go, this has come. You taught the Jade Emperor to let the Heavenly Palace with me. "The Tathagata scold:" You just didn't leave my palm! "The Holy Way:" You do not know. I went to the end of the day, see five sards pillars, holding a green gas, I leave a note there, you dare to go with me to see? "The Tathagata Way:" Do not eliminate, you only look down from the bow. "The Sage opened round eyes, looked down, the original Buddha right hand middle finger wrote" Qi Sheng, to this a tour. "The Great holy Man took a startled way:" There are such things! There's such a thing! I write this word on the pillar of the brace, but how is it on his finger? Is there a foresight spell? I won't believe it! Don't believe it! Wait for me to come again! "Good holy, eager to jump out again, was the Buddha turned the palm of a flutter, the Monkey King launched the West Gate, will be five fingers into gold, wood, water, fire, earth Five mountain, called" Five Elements Mountain, "gently put him down.    we assume that the palm of the Buddha is a circle (so that the Holy One will not fly out of the hand of a 108,000 of wings), the circle of the length of N, counter-clockwise as follows: 0,1,2,...,n-1, and the Holy of the distance of each flight is d. Now the Holy Place is recorded as x, and the great Sage wants to go in Y. Now you need to tell the sage how many wings to go before they reach their destination.
Input
There are multiple sets of test data. The first line is a positive integer t, which represents the number of groups of test data. Each set of test data consists of one row, four nonnegative integers, n (2 < n < 10^9), representing the length of the palm circle of the Tathagata, D (0 < D < n), the distance the wings can fly, x (0 <= x < n), the initial position of the great sage, y (0 <= y &l T n), where the holy Sage wants to go.   Notice that the Sun Wukong's spreading clouds only turn counterclockwise.
Output
For each set of test data, output one line, giving the holy man at least how many wings to turn the cloud to reach the destination. The output "impossible" will not be reached, no matter how many somersault clouds are turned.
Sample_input
23 2 0 23 2 0 1
Sample_output
12
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong Longll;7 voidEXGCD (ll a,ll b,ll& d,ll& x,ll&y)8 {9     if(!b) d=a,x=1, y=0;Ten     ElseEXGCD (b,a%b,d,y,x), y-=x* (A/b); One } A intMain () - { -     intT; thescanf"%d",&T); -      while(t--) -     { - ll N,d,x,y,p,q; +scanf"%lld%lld%lld%lld",&n,&d,&x,&y); - EXGCD (d,n,d,p,q); +d=d<0?-d:d; A         if((y-x)%d!=0) atPuts"Impossible"); -         Else -         { -p*= (y-x)/D; -P= (p% (n/d) + (N/D))% (n/d); -cout<<p<<Endl; in         } -     } to     return 0; +}

Nefu 845-Finger Mountain (extended Euclid)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.