Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. #define ll long long
  4. using namespace std;
  5.  
  6. ll a[1007];
  7. vector<int> v;
  8.  
  9. void solve() {
  10.     int b,n;
  11.     cin>>b>>n;
  12.     for(int i=0;i<b;i++) cin>>a[i];
  13.     if(n <= b) {
  14.         cout<<n<<"\n";
  15.     }
  16.     else {
  17.         v.clear();
  18.         n-=b;
  19.         int K=0;
  20.        
  21.         /*
  22.          * LCM
  23.          */
  24.         ll LCM;
  25.         if(b==1) LCM = a[0];
  26.         else if(b==2) LCM = (a[0]*a[1])/__gcd(a[0],a[1]);
  27.         else {
  28.             LCM = (a[0]*a[1])/__gcd(a[0],a[1]);
  29.             for(ll i=3;i<b;i++) {
  30.                 LCM = (LCM * a[i])/__gcd(LCM,a[i]);
  31.             }
  32.         }
  33.        
  34.         /*
  35.          * Cycle
  36.          */
  37.         for(ll tm=1;tm<=LCM;tm++) {
  38.             for(int i=0;i<b;i++) {
  39.                 if(tm%a[i]==0) {
  40.                     v.pb(i+1);
  41.                     K++;
  42.                 }
  43.             }
  44.         }
  45.        
  46.         /*
  47.          * Modulo
  48.          */
  49.         n %= K;
  50.         cout<<v[(n+K-1) % K]<<"\n";
  51.     }
  52. }
  53.  
  54. int main() {
  55.     int t; cin>>t;
  56.     for(int qq=1;qq<=t;qq++) {
  57.         cout<<"Case #"<<qq<<": ";
  58.         solve();
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement