Spoj LASTDIG2 - The last digit re-visited Solution

    ///La ilaha illellahu muhammadur rasulullah

///******Bismillahir-Rahmanir-Rahim******///

///Abul Hasnat  Tonmoy

///Department of CSE,23rd batch

    ///Islamic University,Bangladesh

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int Modulo(int a, string b)
    4. {
    5. int mod = 0;
    6. for (int i = 0; i <b.size(); i++){
    7. mod = (mod * 10 + b[i] - '0') % a;}
    8. return mod;
    9. }
    10. int LastDigit(string a,string b)
    11. {
    12. int len_a = a.size(), len_b = b.size();
    13. if ((len_a == 1 && len_b == 1 && b[0] == '0' && a[0] == '0')||(len_b == 1 && b[0] == '0')) return 1;
    14. if (len_a == 1 && a[0] == '0') return 0;
    15. int res = pow(a[len_a - 1] - '0', (Modulo(4, b) == 0) ? 4 : Modulo(4, b));
    16. return res % 10;
    17. }
    18. int main()
    19. {
    20. int t,n,i;
    21. cin>>t;
    22. while(t--)
    23. {
    24. string a,b; cin>>a>>b;
    25. cout<<LastDigit(a,b)<<endl;
    26. }
    27. }

    No comments

    Most View Post

    Recent post

    Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

      Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

    Powered by Blogger.