我在桑拿大学603奇妙经历 B - 快速幂
题目
People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow:
Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions AiBi from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers.
You should write a program that calculates the result and is able to find out who won the game.
Input
The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 <= M <= 45000). The sum will be divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.
Output
For each assingnement there is the only one line of output. On this line, there is a number, the result of expression
(A1B1+A2B2+ ... +AHBH)mod M.
Sample Input
3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132
Sample Output
2
13195
13
Sponsor
思路
英文 阿巴阿巴
翻译(有道yyds)
人是不同的。 有些人偷偷地看满是有趣女孩照片的杂志,有些人在地窖里制造原子弹,有些人喜欢使用窗户,还有一些人喜欢复杂的数学游戏。 最新的市场调查显示,这一细分市场迄今为止被低估了,这类游戏并不存在。 这类游戏因此被列入KOKODáKH。 规则:
每个玩家选择两个数字Ai和Bi,并把它们写在一张纸上。 其他人看不到这些数字。 在一个特定的时刻,所有的玩家都向其他人展示他们的数字。 其目标是确定包括自己在内的所有玩家的所有表达式AiBi的总和,并确定除给定数字m后的余数。首先确定正确结果的人将获胜。 根据玩家的经验,可以通过选择更高的数字来增加难度。
你应该写一个程序来计算结果,并能够找出谁赢了这场比赛。
输入
输入由Z赋值组成。 它们的数量由出现在输入第一行的单个正整数Z给出。 接下来是作业。 每个赋值都以包含整数M (1 <= M <= 45000)的行开始。 总和要除以这个数。 下一行包含玩家数量H (1 <= H <= 45000)。 接下来正好是H线。 在每一行上,有两个用空格隔开的数字“艾”和“比”。 两个数字不能同时等于零。
思路
其实这个问题很简单
先来个入门的小题目
题目描述:
求 aa 的 bb 次方对 pp 取模的值。
输入格式
三个整数 a,b,pa,b,p ,在同一行用空格隔开。
输出格式
输出一个整数,表示a^b mod p的值。
数据范围
1≤a,b,p≤10^9
输入样例:
3 2 7
输出样例:
2
如果直接循环的话,10的9次方肯定会超时,我们先写几组数据找一下规律
输入
3 7 10
7的二进制就是111
3^1=3
3^2=9
3^3=81
3^7=3*9*81
把b变成2,有几位就先对a预处理几位,这样后面的都可以由前面的得到,复杂度就是log2n;
————————————————
版权声明:本文为CSDN博主「邵光亮」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43627087/article/details/96877014
解:
#include<iostream>
using namespace std;
int main() {
int a,b,p;
cin>>a>>b>>p;
int ans = 1%p;
while(b){
if(b&1) {
ans = ans * 1ll * a % p;
}
a = a*1ll*a%p;
b>>=1;
}
cout<<ans<<endl;
//std::cout << "Hello, World!" << std::endl;
//return 0;
}
如果你能理解上面的题 你就能了解快速幕的核心
答
#include <stdio.h>
#define ll long long
ll ksm(ll a, ll b, ll mod)
{
ll ans;
if(b==0){
return 1;
}
ans=ksm(a*a%mod,b/2,mod);
if(b%2==1){
ans=ans*a%mod;
}
return ans;
}
int main()
{
ll z;
ll m,h,a,b;
ll ans=0;
ll i;
scanf("%lld",&z);
while(z--)
{
ans=0;
scanf("%lld %lld",&m,&h);
for(i=0;i<h;i++)
{
scanf("%lld%lld",&a,&b);
ans=(ans+ksm(a,b,m))%m;
}
printf("%lld\n",ans);
}
return 0;
}
这篇博客介绍了如何利用快速幂算法解决一个有趣的数学游戏问题,参与者通过计算AiBi的和并求模M,利用高效计算技巧赢得比赛。作者提供了一个C++程序示例,展示了如何在给定输入条件下计算结果并确定赢家。

2983

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



