http://acm.hdu.edu.cn/showproblem.php?pid=1061
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
#include "algorithm"
#include "iostream"
using namespace std;
#define INT __int64
int Pow( INT n )
{
int temp = n % 10 ;
int ans = 1 ;
while( n )
{
if( n % 2 == 1 )
{
n-- ;
ans *= temp;
ans %= 10 ;
}
else
{
n /= 2 ;
temp *= temp ;
temp %= 10 ;
}
ans %= 10 ;
}
return ans ;
}
int main()
{
INT n ;
int Case ;
cin >> Case ;
while( Case-- )
{
cin >> n ;
cout << Pow( n ) << endl ;
}
return 0;
}
下面是发现没20个数据存在循环,之前没有将num[ 0] 赋值为0 ,总是WA,还怀疑规律是不是错了,但是突然发现20的倍数mod20会为0
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
__int64 n ;
int a , b , num[ 50 ] ;
int i ;
num[ 0 ] = 0 ;
for(i = 1 ; i <= 20 ; i++ )
{
a = i ;
b = 1 ;
while ( a-- )
{
b = b * i % 10 ;
}
num[ i ] = b ;
}
int Case ;
cin >> Case;
{
while( Case-- )
{
cin >> n ;
n = n % 20 ;
// cout << n << endl ;
cout << num[ n ] << endl ;
}
}
return 0 ;
}
290



被折叠的 条评论
为什么被折叠?



