CodeForces 742A Arpa’s hard exam and Mehrdad’s naive cheat

题意:输出1378的 n 次方的个位数

  • 实际上8 n <script type="math/tex" id="MathJax-Element-51">n</script>次幂是存在循环节的,但为了方便直接就用快速幂做了

    #include <cstdio>
    #include <string>
    #include<iostream>
    #include<vector>
    #include <stack>
    #include <queue>
    #include <map>
    #include <cstdlib>
    #include<string.h>
    #include <cstring>
    #include <ctime>
    #include <algorithm>
    #include <set>
    
    using namespace std;
    
    typedef long long ll;
    typedef pair<int, int>pii;
    typedef pair<ll, ll> pll;
    typedef pair<int, ll> pil;
    typedef vector<vector<ll> >vvi;
    typedef vector<ll> vi;
    
    const ll mod = 1e9 + 7;
    const int MAXN = 1000;
    const int MAXM = 1000;
    
    ll pow_mod(ll a, ll n, ll mod)
    {
        ll res = 1;
        while (n)
        {
            if (n & 1)res = res*a%mod;
            a = a*a%mod;
            n >>= 1;
        }
        return res;
    }
    
    ll gcd(ll x, ll y)
    {
        return y == 0 ? x : gcd(y, x%y);
    }
    
    ll lcm(ll x, ll y)
    {
        return x / gcd(x, y)*y;
    }
    
    struct Edge
    {
        int to; int next;
        int cal;
    }edge[MAXM];
    
    int head[MAXN], tot;
    
    void addedge(int u, int v, int cal)
    {
        edge[tot].to = v;
        edge[tot].cal = cal;
        edge[tot].next = head[u];
        head[u] = tot++;
    }
    
    void init()
    {
        tot = 0;
        memset(head, -1, sizeof head);
    }
    
    struct Matrix
    {
        int m[10][10];
        int n;
        void E()
        {
            for (int i = 0; i < 10; i++)for (int j = 0; j < 10; j++)
                m[i][j] = i == j;
        }
        void clear()
        {
            memset(m, 0, sizeof m);
        }
        Matrix operator*(const Matrix b)
        {
            Matrix res;
            res.n = b.n;
            res.clear();
            for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)
            {
                res.m[i][j] = 0;
                for (int k = 0; k < n; k++)
                    res.m[i][j] = (res.m[i][j] + m[i][k] * b.m[k][j] % mod) % mod;
            }
            return res;
        }
    
        Matrix operator^(ll tim)
        {
            Matrix a = *this;
            Matrix res;
            res = a;
            res.E();
            while (tim)
            {
                if (tim & 1)res = res*a;
                a = a*a;
                tim >>= 1;
            }
            return res;
        }
    };
    
    int cot[100000 + 10];
    int main()
    {
        int n;
        cin >> n;
        cout << pow_mod(1378, (ll)n, 10) % 10 << endl;
        //system("pause");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值