fork download
  1. #include <stdio.h>
  2. typedef long long ll;
  3. ll num_calc(ll x,ll y,ll n);
  4. char str[300001];
  5. ll dir_x[4]={-1,1,0,0};
  6. ll dir_y[4]={0,0,-1,1};
  7. ll go[128];
  8. int main(void)
  9. {
  10. ll n,Q,x=0,y=0,ret=0; go['U']=0; go['D']=1; go['L']=2; go['R']=3;
  11. scanf("%lld%lld\n",&n,&Q); scanf("%s",str);
  12. for(int i=0;i<Q;i++)
  13. {
  14. //UDLR
  15. x=x+dir_x[go[str[i]]];
  16. y=y+dir_y[go[str[i]]];
  17. ret=ret+num_calc(x,y,n);
  18. }
  19. printf("%lld\n",ret+1);
  20. return 0;
  21. }
  22. ll num_calc(ll x,ll y,ll n)
  23. {
  24. ll u=x+y,temp,hang;
  25. if(u<n)
  26. {
  27. temp=(u*(u+1))/2;
  28. if(u&1)
  29. return (temp+u+1-y);
  30. else
  31. return temp+y+1;
  32. }
  33. else
  34. {
  35. //(1) 1~n까지 더한다.
  36. temp=(n*(n+1))/2;
  37. //(2) 항 갯수 구한다.
  38. hang=u-n;
  39. //(3) 초항 : n-1 말항 : n-hang
  40. temp=temp+(hang*(n-1+n-hang))/2;
  41.  
  42. //(4) y축 기준선 잡기
  43. if(u&1)
  44. return (temp+n-y);
  45. else
  46. {
  47. //n-hang-1을 더한 후에..
  48. temp=temp+n-hang-1;
  49. // 기준선을 x=n-1로 잡자.
  50. temp=temp-(n-1-y);
  51. return temp;
  52. }
  53. }
  54. }
Success #stdin #stdout 0s 10600KB
stdin
20 114
RRRRLRRRRRRRRRRRRRRRRDDDDDDDDDDDDLLRRDDDDDDUUDDDLLRLLLLLULDLLLLLLUDLUDRLULLLDLLUDUUUUUUDRULUUUUURLUUUUDURLURLUURUL
stdout
23022