安全密码

问题描述:
密码在我们日常生活中经常要用到,如邮箱密码、QQ密码等,设置一般的密码很容易破解的哦,千万不要用你的出生年月作为你的密码,那样很不安全。小K用到了一种比较安全的密码设置方法:密码由三个正整数a,b,c经过计算a的b次方除以c的余数得到。现在请你编写一个程序,计算a^b mod c 的值。
数据输入:
从文件password.in中读入数据,文件中只有一行,依次为三个正整数a,b,c,三个正整数之间用空格隔开。
数据输出
结果输出到文件password.out中,只有一个数,表示计算得到的结果。
输入输出样例

password. in
2 3 7

password.out
1
数据范围说明:
60%的数据中,a的b次方的值在longint范围内。
70%的数据中,a的b次方的值在int64范围内.
100%的数据中,a,b,c 的值小于1000 。

.
.
.
.
.
分析
这是一个快速幂的模板

.
.
.
.
.
程序:

#include<iostream> 
#include<cstdio>
#include<cstring>
using namespace std;

int power(int a,int b,int p)
{
	int ans=1%p;
	for (;b;b=b>>1)
	{
		if (b&1) ans=(long long)ans*a%p;
		a=(long long)a*a%p;
	}
	return ans;
}

int main()
{
	freopen("password.in","r",stdin);
	freopen("password.out","w",stdout);
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	int ans;
	ans=power(a,b,c);
	printf("%d",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}
posted @ 2019-05-08 19:33  银叶草  阅读(491)  评论(0编辑  收藏  举报
Live2D