Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define ll long long
- using namespace std;
- ll a[1007];
- vector<int> v;
- void solve() {
- int b,n;
- cin>>b>>n;
- for(int i=0;i<b;i++) cin>>a[i];
- if(n <= b) {
- cout<<n<<"\n";
- }
- else {
- v.clear();
- n-=b;
- int K=0;
- /*
- * LCM
- */
- ll LCM;
- if(b==1) LCM = a[0];
- else if(b==2) LCM = (a[0]*a[1])/__gcd(a[0],a[1]);
- else {
- LCM = (a[0]*a[1])/__gcd(a[0],a[1]);
- for(ll i=3;i<b;i++) {
- LCM = (LCM * a[i])/__gcd(LCM,a[i]);
- }
- }
- /*
- * Cycle
- */
- for(ll tm=1;tm<=LCM;tm++) {
- for(int i=0;i<b;i++) {
- if(tm%a[i]==0) {
- v.pb(i+1);
- K++;
- }
- }
- }
- /*
- * Modulo
- */
- n %= K;
- cout<<v[(n+K-1) % K]<<"\n";
- }
- }
- int main() {
- int t; cin>>t;
- for(int qq=1;qq<=t;qq++) {
- cout<<"Case #"<<qq<<": ";
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement