ACM 1061:Rightmostcheck digitt,为什么老是提示错误呢?改了十几遍了,简直没法做了,求教高人。

2056人阅读
编程心得(8)
&题目是这样的
Problem DescriptionGiven a positive integer N, you should output the most right digit of N^N.
InputThe input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.Each test case contains a single positive integer N(1&=N&=1,000,000,000).
OutputFor each test case, you should output the rightmost digit of N^N.
Sample Input234&
Sample Output76
HintIn the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.由于有时间限制和运算范围的限制,显然不能直接计算,那么就是要找规律。我们知道,这最后一个数只是最后个位数反复相乘的结果,而这结果只在0与9之间,我们只要计算出它的循环长度即可。代码如下:
#include &iostream&
int main()
int i=0,N;
for (i=0;i&T;i++)
int M=N%10,P=1,MM,len=0;
int T[10]={10};
for(int j=0;j&N;j++)
MM=(P*M)%10;
for(int k=0;k&=k++)
if(T[k]==MM)
goto MARK;
T[len++]=MM;
cout&&T[(N+len-1)%len]&&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:75548次
积分:1237
积分:1237
排名:千里之外
原创:45篇
评论:12条
(1)(1)(4)(11)(1)(2)(6)(4)(5)(4)(2)(2)(1)(1)(4)(1)(1)acm之路--数学(263)
Time Limit:
MS (Java/Others)&&&&
Problem Description
Sample Input
Sample Output
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
找规律吧:
基本代码:
/*0ms,228KB*/
#include&cstdio&
int ans(int temp)
switch (temp)
if (y == 0)
switch (y)
if (y == 0)
switch (y)
if (y == 0)
switch (y)
if (y == 0)
switch (y)
if (y == 0)
switch (y)
if (y == 0)
switch (y)
int main(void)
scanf(&%d&, &t);
while (t--)
scanf(&%d&, &y);
int temp = y % 10;
printf(&%d\n&, ans(temp));
更好的规律:
/*0ms,228KB*/
#include&cstdio&
const int ans[20] = { 0, 1, 4, 7, 6, 5, 6, 3, 6, 9, 0, 1, 6, 3, 6, 5, 6, 7, 4, 9 };
int main(void)
scanf(&%d&, &T);
while (T--)
scanf(&%d&, &N);
printf(&%d\n&, ans[N % 20]);
规律的综合公式:
/*0ms,228KB*/
#include &cstdio&
int main(void)
int t, n, p,
scanf(&%d&, &t);
while (t--)
scanf(&%d&, &n);
p = n % 10;
for (int i = 0; i & (n + 3) % 4; i++)
printf(&%d\n&, sum % 10);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:639197次
积分:13729
积分:13729
排名:第595名
原创:704篇
转载:42篇
评论:104条
(67)(76)(26)(29)(97)(125)(154)(160)(13)快速幂(1)

Rightmost Digit
Time Limit:
MS (Java/Others)&&&&Memory Limit:
K (Java/Others)
Total Submission(s): 36563&&&&Accepted Submission(s): 13871
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1&=N&=1,000,000,000).
For each test case, you should output the rightmost digit of N^N.
Sample Input
Sample Output
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
Ignatius.L
求N的N次方的最后一位数。
快速幂。。
假如8^4。可以看成64^2&& 。。
这样运算次数减少了三次。
假如8^5 我们可以先将一个8存起来,变成8^4。。
然后一直反复操作。。
#include &stdio.h&
#include &cmath&
#include &vector&
#include &map&
#include &time.h&
#include &cstring&
#include &set&
#include&iostream&
#include &queue&
#include &stack&
#include &algorithm&
#define inf 0x6f6f6f6f
#define mod 10
long long wei(long long k)
long long n=k;
long long r=1;
//k次幂为奇数,可以存起一个数。
//位运算,加快速度。
int main()
scanf(&%d&,&t);
while(t--)
long long ans=wei(k);
cout&&ans&&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:73517次
积分:3497
积分:3497
排名:第6550名
原创:284篇
评论:12条
(9)(5)(12)(1)(9)(17)(27)(11)(16)(6)(5)(23)(29)(29)(33)(56)_____数论_______(9)
Problem Description
Sample Input
Sample Output
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
//============================================================================
: Math_hdu1061.cpp
// Version
// Copyright
: Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include &iostream&
int nu[10] = { 1, 1, 4, 4, 2, 1, 1, 4, 4, 2 };//找规律
int main() {
long long a, temp,
while (t--) {
a = temp % 10;
if (nu[a] == 1) {
cout && a &&
else if (a == 4) {
cout && '6' &&
else if (a == 9){
cout && '9' &&
b = temp % nu[a];
if(a == 2){
switch(b){
cout && '6' &&
cout && '2' &&
cout && '4' &&
cout && '8' &&
else if(a == 3){
switch(b){
cout && '1' &&
cout && '3' &&
cout && '9' &&
cout && '7' &&
else if(a == 8){
switch(b){
cout && '6' &&
cout && '8' &&
cout && '4' &&
cout && '2' &&
else if(a == 7){
switch(b){
cout && '1' &&
cout && '7' &&
cout && '9' &&
cout && '3' &&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:186216次
积分:4179
积分:4179
排名:第5053名
原创:221篇
转载:47篇
评论:66条
文章:14篇
阅读:11816
文章:78篇
阅读:57258Acm_奋斗乐园2010(25)
// hdoj_1061 Rightmost Digit 右值
// 0MS 228K 253 B GCC
#include &stdio.h&
int main(vodi)
int i, ncase, m, n,
scanf(&%d&, &ncase);
while(ncase--)
scanf(&%ld&, &n);
t = n % 10;
for(i = 0; i &= (n-1)%4; i ++)& //此处可换为 i & n%4 + 4;可解决n%4 ==0的情况
printf(&%d\n&, m%10);
相乘后的末位数
9(由于x9是奇数,所以1不会出现)
由上面的分析可见,每个数相乘后最多有四个结果
所以对一个数n,只需做其对4取余后余数次相乘即可
但是会出现 n%4 == 0 的情况
由于x5^x5的末位数是5,x9^x9的末位数是9
所以将n%4转换为(n-1)%4
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:66933次
积分:1104
积分:1104
排名:千里之外
原创:42篇
评论:44条
(1)(2)(1)(25)(17)

我要回帖

更多关于 check digit 的文章

 

随机推荐