Extended Euclidean Wuzhishan NEFU 84

Source: Internet
Author: User

Wuzhishan

Time limit 1000 ms Memory limit 65536 K
Description
Sun Wukong, who is traveling to the West, is in the tiangao Palace. If he comes to the Buddha and falls down to him, he said, "I am playing a bet with you. If you have the skill, you will win in my right hand, if you don't need to fight, you need to ask the emperor to live in the West and let you know about the tiangu. If you can't beat your palm, you will also fall down to the demon and commit a few more robberies, but you will quarrel." The secret said with a smile: "This is a wonderful time! My old grandson went to 108,000 miles. How can I not jump out of the palm of his hand ?" In an emergency, he said, "Can you make your claim ?" Buddha said: "Do it! Done !" Extend your right hand, but it looks like a lotus leaf. The secret of the secret was as awesome as it was, and the secret of the secret was shaken up, standing in the palm of the Buddha's hand, shouting, "I am going out !" You can see that all the way to the cloud is invisible. During the big holy line, I suddenly saw five red meat pillars, holding a green spirit. He said, "This is the end. If I come back to testify, I will definitely sit here ." Turning over the power of the cloud, walking back to the place, standing in the palm of the hand: "I have been here, now. You taught the Emperor tiangu and me ." As you said, "You have never left me !" Dasheng said: "You don't know. I went to the end of heaven and saw five red meat columns. I kept them there. Do you dare to check them with me ?" For example, "No elimination, you just look down ." When the big Sage glared at his eyes and looked down, the Buddha's right-hand middle finger said, "Qi tiandasheng, come here for a tour ." Dasheng was shocked and said, "This is the case! This is the case! How can I write this word on the pillar of the sky, but on his fingers? Is there an unknown prophet spell? I will never believe it! Believe it! Come back later !" Good big Sheng, eager to jump out again, the Buddha throws a throw, the Monkey King launched outside the westday door, five fingers into gold, wood, water, fire, earth five joint mountains, call the name "wuxingshan" and gently hold him down. Let us assume that the palm of the Buddha is a circle (so the palm of the hand cannot fly out of the palm of the great holy spirit). The circle length is N, which is recorded as 0, 1, 2 ,..., N-1, while the distance between the great saints is D. Now the location of the great saints is counted as X, and the place where the great saints want to go is Y. Now, you need to tell dasheng how many resources are required to reach the destination.
Input
There are multiple groups of test data. The first line is a positive integer T, indicating the number of test data groups. Each group of test data includes one row, four non-negative integers, N (2 <n <10 ^ 9), indicating the length of the Palm Circle, D (0 <D <n ), distance to which the artifact can fly; x (0 <= x <n); initial location of the great saint; y (0 <= Y <n); location where the great saint wants to go. Note that Sun Wukong's Jin douyun only goes counter-clockwise.
Output
For each group of test data, output a row and give the minimum number of tables and muscles to be turned over to reach the destination. If no matter how many teams can be reached, output "impossible ".
Sample_input
23 2 0 23 2 0 1
Sample_output
12


#include <iostream>using namespace std;typedef long long int64;int64 ex_gcd(int64 a,int64 b, int64 & x, int64 & y){if(b == 0){x = 1;y = 0;return a;}else{int64 r = ex_gcd(b, a%b, x, y);int64 t = x;x = y;y = t - a/b * y;return r;}}int64 gcd(int a,int b){if(b==0)return a;return gcd(b, a%b);}int main() {int64 k;cin >> k;int64 n,m,x,y;while(k--){cin >> n >> m >> x >> y;int64 a = m;int64 b = n;int64 d = (y - x + n) % n; // ax + by = dint64 X0,Y0;int64 gcd = ex_gcd(a,b, X0, Y0);cout << X0 << endl;cout << gcd << endl;int64 r = b / gcd;if(d % gcd)cout << "Impossible" << endl;elsecout <<( d/ gcd * X0 % r + r) % r << endl;}return 0;}

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.