HDU 1061

    xiaoxiao2023-03-24  5

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061

    一上来先无脑每次乘完用10取余,提交了一遍,果然超时。

    然后分析发现各个数的周期是1、2、4 然后乱搞了一下居然过了!!! 不过把1024换成512就会wa

    #include<iostream> using namespace std; int main() { long long n,m,i,j,flag; cin>>n; for(i=0;i<n;i++) { cin>>m; flag=m%10; m=m%1024; int t=1; for(int j=0;j<m;j++) t=t*flag%10; cout<<t<<endl; } return 0; }

    最稳妥的方法当然是打表啦!

    #include<iostream> using namespace std; int main() { long long m; int n,p; int a[10][4] = {{0},{1},{6,2,4,8},{1,3,9,7},{6,4},{5},{6},{1,7,9,3},{6,8,4,2},{1,9}}; scanf("%d",&n); while(n--) { scanf("%lld",&m); p=m%10; if(p==0 || p==1 || p==5 || p==6) printf("%d\n",a[p][0]); if(p==4 || p==9) printf("%d\n",a[p][m%2]); if(p==2 || p==3 || p==7 || p==8) printf("%d\n",a[p][m%4]); } return 0; }

    还可以用快速幂,做的时候并不会实现,参考了一下网上的快速幂实现方法,代码如下:

    #include<iostream> using namespace std; int qp(long long n) { int res,t; res=1;t=n%10; while(n) { if(n & 1) res=res*t%10; t=t*t%10; n>>=1; } return res; } int main() { int Case; long long n; scanf("%d",&Case); while(Case--) { scanf("%lld",&n); printf("%d\n",qp(n)); } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1201789.html
    最新回复(0)