Save the Prisoner
A jail has prisoners, and each prisoner has a unique id number, , ranging from to . There are sweets that must be distributed to the prisoners.
The jailer decides the fairest way to do this is by sitting the prisoners down in a circle (ordered by ascending ), and then, starting with some random , distribute one candy at a time to each sequentially numbered prisoner until all candies are distributed. For example, if the jailer picks prisoner , then his distribution order would be until all sweets are distributed.
But wait—there's a catch—the very last sweet is poisoned! Can you find and print the ID number of the last prisoner to receive a sweet so he can be warned?
Input Format
The first line contains an integer, , denoting the number of test cases.
The subsequent lines each contain space-separated integers:
(the number of prisoners), (the number of sweets), and (the prisoner ID), respectively.
- Solution
- In C++ Language
#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
ull n,m,s;
cin>>n>>m>>s;
ull ans = (s+m-1)%n;
if(ans!=0)
cout<<ans<<"\n";
else
cout<<n<<"\n";
}
return 0;
}
- In Java Language
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
static int check(int a,int b,int c){
int k=(a+b-1)%c;
if(k==0)return c;
return k;
}
public static void main (String[] args) throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out));
int t=Integer.parseInt(br.readLine());
while(t-->0){
String s1[]=br.readLine().split(" ");
int p=Integer.parseInt(s1[0]);
int s=Integer.parseInt(s1[1]);
int po=Integer.parseInt(s1[2]);
bw.write(check(po,s,p)+"\n");
}
bw.flush();
}
}
No comments: