Tophat

Codechef magic trick problem coded in C!

Jan 13th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1.     #include<stdio.h>
  2.     #include<string.h>
  3.     typedef unsigned long long int ll;
  4.     ll a[1000000];
  5.     char s[1000000];
  6.     ll mul(ll a1,ll b1,ll c1)
  7.     {
  8.     ll pro=0;
  9.     b1=b1%c1;
  10.     while(a1)
  11.     {
  12.     if(a1&1)
  13.     {
  14.     pro=pro+=b1;
  15.     if(pro>c1) pro=pro-c1;
  16.     }
  17.     a1>>=1;
  18.     b1<<=1;
  19.     if(b1>c1)
  20.     b1=b1-c1;
  21.     }
  22.     return pro;
  23.      
  24.     }
  25.      
  26.     #define getcx getchar_unlocked
  27.     inline void fscan(ll *a )
  28.     {
  29.     ll n=0; int ch = getcx(); int sign = 1;
  30.     while(ch < '0' || ch > '9')
  31.     {
  32.     if(ch == '-') sign=-1; ch = getcx();
  33.     }
  34.     while(ch >= '0' && ch <= '9')
  35.     {
  36.     n = (n << 3) + (n << 1) + ch - '0', ch = getcx();
  37.     }
  38.     *a = n * sign;
  39.     }
  40.      
  41.     int main()
  42.     {
  43.     ll i,j,k,l,m,n,t,x,y,z,temp1,temp2;
  44.     fscan(&t);
  45.      
  46.     while(t--)
  47.     {
  48.     fscan(&n);
  49.     for(i=0;i<n;i++)
  50.     fscan(&a[i]);
  51.     fscan(&x);
  52.     fscan(&y);
  53.     fscan(&z);
  54.     scanf("%s",s);
  55.      
  56.      
  57.      
  58.     l=strlen(s);
  59.     ll end=n-1,count=n,min=0,max=n-1,revcount=0;
  60.     i=0;
  61.     for(count=0;count<n;count++)
  62.     {
  63.      
  64.     if(s[count]=='A')
  65.     {
  66.     for(j=min;j<=max;j++)
  67.     {
  68.     a[j]=(a[j]+x)%z;
  69.     }
  70.     printf("%lld ",a[i]);
  71.     if(((revcount&1))==1) i--;
  72.     else i++;
  73.     }
  74.      
  75.     else if(s[count]=='M')
  76.     {
  77.     for(j=min;j<=max;j++)
  78.     {
  79.     a[j]=mul(a[j],y,z);
  80.     }
  81.     printf("%lld ",a[i]);
  82.     if(((revcount&1))==1) i--;
  83.     else i++;
  84.      
  85.     }
  86.     else
  87.     {
  88.     printf("%lld ",a[end]%z);
  89.     if(end>=i)
  90.     temp1=end-1;
  91.     else
  92.     temp1=end+1;
  93.      
  94.     temp2=temp1;
  95.     temp1=i;
  96.     i=temp2;
  97.     end=temp1;
  98.     revcount++;
  99.     if(end>i)
  100.     {
  101.     min=i;max=end;
  102.     }
  103.     else
  104.     {
  105.     min=end;max=i;
  106.     }
  107.     }
  108.      
  109.     }
  110.      
  111.     printf("\n");  
  112.     }
  113.     return 0;
  114.     }
Advertisement
Add Comment
Please, Sign In to add comment