首页 > 代码库 > UVa 10515 Powers Et Al.

UVa 10515 Powers Et Al.

方法:数论

注意last digit只与m的最后一位和m有关,同时,如果了解number theory 中的multiplicative order 的话,就会发现,1-9 mod 10 的order 不是1 就是4, 所以结果只与m的最后一位和n mod 4的结果有关。而n mod 4 的结果只与 n的最后后两位有关。注意如果n % 4 = 0, 需要n += 4 ,否则会得到m^0 = 1。

code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <unordered_map>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#include <iomanip>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline ‘\n‘

#define test if(1)if(0)cerr
using namespace std;
  using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);

string s1, s2;
int m, n;

int main()
{
    while (cin >> s1 >> s2)
    {
        if (s1[0] == ‘0‘ && s2[0] == ‘0‘) break;
        m = s1.back()-‘0‘;
        if (m == 0)
        {
            cout << 0 << newline;
            continue;
        }
        if (s2[0] == ‘0‘)
        {
            cout << 1 << newline;
            continue;
        }
        n = s2.back()-‘0‘;
        s2.pop_back();
        if (s2.length() > 0)
            n += 10*(s2.back()-‘0‘);
        n += 4;
        int ret = m;
        for (int i = 1; i < n; ++i)
            ret = ret * m % 10;
        cout << ret << newline;
            
    }
}

  

UVa 10515 Powers Et Al.