HDU-1395-2^x mod n = 1(数学题(二次出错))

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1395

这题一定要滴水不漏的把所有代码全部看完。

这个题目是一个数学类型的题,我也没思路,只知道n==1||n%2==0时,x是找不到的,其他则不知道还有没有n存在x是找不到的,

还有一个,暴力搜索时不知道搜到哪里为止,提交,则超时,看了别人的博客:如果a和n互质且a<n则a^x%n=1,因此在此题中,a为2

所以只要a!=1&&a%2!=0,则一定存在x,

#include<stdio.h>
int main(void)
{
int s,n;
while(scanf("%d",&n)==1)
{
if(n%2==0||n==1)
{
printf("2^? mod %d = 1\n",n);
continue;
}
int m=2;
s=1;
int k=0;
for(;;)
{
s=s*m;
k++;
if(s==n+1)
{
printf("2^%d mod %d = 1\n",k,n);
break;
}
if(s>n+1)
s=s%n;//防止数据过大溢出

}

}
return 0;
}

另一种解法

#include<stdio.h>
int main(void)
{
int s,n,i;
int m;
while(scanf("%d",&n)==1)
{
if(n==1||n%2==0)
{
printf("2^? mod %d = 1\n",n);
continue;
}
s=1;
m=2;
for(i=1;i<=n;i++)
{
s=s*m;
if(s%n==1)
break;
s=s%n;//防止溢出;
}
if(i<=n)
printf("2^%d mod %d = 1\n",i,n);
else
printf("2^? mod %d = 1\n",n);
}
return 0;
}

解释

网友问答

为什么是最多执行 n 次呢?不好意思反应不过来^_^

2^k % n 最多只有n种不同结果之后就会开始循环

所以最多只需要尝试n次,如果有解的话n次以内就有解,n次以内没有解的话再往后面试也不可能有解

第二种的修改

#include<stdio.h>
int main(void)
{
int s,n,i;
int m;
while(scanf("%d",&n)==1)
{
if(n==1||n%2==0)
{
printf("2^? mod %d = 1\n",n);
continue;
}
s=1;
m=2;
for(i=1;i<=n;i++)
{
s=s*m;
if(s%n==1)
break;
s=s%n;
}
if(i<=n)
printf("2^%d mod %d = 1\n",i,n);
//else
//printf("2^? mod %d = 1\n",n);
}
return 0;
}

同样也会过(AC)第一种方法可解释;

以上全是他人的,别人的毕竟是别人的,只有自己的才会更深刻

WA  Time Limit Exceeded

#include<stdio.h>
#include<math.h>
int main(void)
{
int s,n,i;
int m;
while(scanf("%d",&n)==1)
{
if(n==1||n%2==0)
{
printf("2^? mod %d = 1\n",n);
continue;
}
s=1;
m=2;
for(i=1;i<=n;i++)
{
s=(int)(pow(2,i));
if(s%n==1)
break;
s=s%n;
}
if(i<=n)
printf("2^%d mod %d = 1\n",i,n);
}
return 0;
}

为什么会  Time Limit Exceeded,主要是s=(int)(pow(2,i));两个方面(一)强制转换耗时,(二)pow函数耗时,导致Time Limit Exceeded。

时间: 2024-10-09 22:38:46

HDU-1395-2^x mod n = 1(数学题(二次出错))的相关文章

hdu 1395 2^x mod n = 1 (简单数论)

题目大意: 求出一个最小的x 使得 2的x次方对n取模为1 思路分析: 若要 a*b%p=1  要使得b存在 则 gcd (a,p)=1. 那么我们应用到这个题目上来. 当n为偶数 2^x 也是偶数,那么gcd 肯定不是1.故这个是不存在的. 那么n为奇数的时候,也就一定是1了. 所以直接暴力找. #include <iostream> #include <cstdio> using namespace std; int main() { int n; while(scanf(&q

HDU 1395 2^x mod n = 1

/* 中文题意: 中文翻译: 题目大意:求出最小的 n 使得2的 I 次方对 n 的值为1. 解题思路:如下: 难点详解:先用费马小定理了解2的 i 次方对偶数取余都不可能是一,还有就是排除 1 .之后要用中国剩余定理让 t 的值不超出 int 范围.不用这个定理我错了n次,都是超时.我猜测可能是 t 的值超出了int 的范围了,之后的数都是随机的,所以一直运行不出来,才会超时的.(不知道我的猜测对不对,欢迎大家指正) 关键点:理解费马小定理(我到现在还是不理解),只是用到了一点点这个东西.还有

hdu 1395 2^x mod n = 1(暴力题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12146    Accepted Submission(s): 3797 Problem Description Give a number n, find

hdu 1395 2^x mod n = 1 暴力过~~最好学下欧拉定理~~~

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13341    Accepted Submission(s): 4143 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. In

hdu 1395 2^x mod n = 1(欧拉函数)

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18742    Accepted Submission(s): 5860 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. Inp

hdu 4974 A simple water problem(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4974 Problem Description Dragon is watching competitions on TV. Every competition is held between two competitors, and surely Dragon's favorite. After each competition he will give a score of either 0 or

HDU 3105 Fred&#39;s Lotto Tickets(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3105 Problem Description Fred likes to play the lotto. Whenever he does, he buys lots of tickets. Each ticket has 6 unique numbers in the range from 1 to 49, inclusive. Fred likes to ``Cover all his base

(HDU)1395 -- 2^x mod n = 1

题目链接:http://vjudge.net/problem/HDU-1395 分析可知,n为偶数或者1的时候输出'?' 自己设置一个循环周期. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <string> 7 #include <cstdli

杭电 HDU ACM 1395 2^x mod n = 1

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13610    Accepted Submission(s): 4208 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. In

2^x mod n = 1 HDU - 1395(欧拉定理 原根)

2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20711    Accepted Submission(s): 6500 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. Inp