zoukankan      html  css  js  c++  java
  • 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    There exists an island called Arpa’s land, some beautiful girls live there, as ugly ones do.

    Mehrdad wants to become minister of Arpa’s land. Arpa has prepared an exam. Exam has only one question, given n, print the last digit of 1378n.

    Mehrdad has become quite confused and wants you to help him. Please help, although it’s a naive cheat.

    Input
    The single line of input contains one integer n (0  ≤  n  ≤  109).

    Output
    Print single integer — the last digit of 1378n.

    Examples
    input
    1
    output
    8
    input
    2
    output
    4
    Note
    In the first example, last digit of 13781 = 1378 is 8.

    In the second example, last digit of 13782 = 1378·1378 = 1898884 is 4.

    【题目链接】:http://codeforces.com/problemset/problem/742/A

    【题解】

    快速幂取模;
    把那个数用快速幂乘一下;乘的时候取个位数就好;

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    void rel(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void rei(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    LL n;
    LL ans = 1;
    
    void f(LL x)
    {
        if (x==0)
            return;
        f(x>>1);
        ans = (ans * ans)%10;
        if (x&1)
            ans=(ans*1378)%10;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        cin>>n;
        if (n==0)
            puts("1");
        else
        {
            f(n);
            cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    linux IPtable防火墙 禁止和开放端口
    Forward链、Input链和Output链的区别
    linux下导入、导出mysql数据库命令
    linux 环境下tomcat中部署jfinal项目
    android 调用系统界面
    效果超赞的基于js的chat组件和图形动画组件
    《Tsinghua oc mooc》第5~7讲 物理内存管理
    《Tsinghua os mooc》第1~4讲 启动、中断、异常和系统调用
    《MIT 6.828 Homework 2: Shell》解题报告
    《xv6 Appendices: PC Hardware and Boot loader》学习笔记
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626859.html
Copyright © 2011-2022 走看看