poj1995 Raising Modulo Numbers【高速幂】
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5500 | Accepted: 3185 |
Description
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
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
(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
#include<stdio.h>
#include<string.h>
int ksm(long long a,long long b,long long n)
{
long long ans=1;
while(b)
{
if(b&1)
ans=ans*a%n;
//写成ans*=a%n不行...输出错误
a=a*a%n;
b>>=1;
}
return ans;
}
int main()
{
int z,h,m,u,i;
scanf("%d",&z);
while(z--)
{
int sum = 0;
scanf("%d",&m);
scanf("%d",&h);
while(h--)
{
scanf("%d%d",&u,&i);
sum += ksm(u,i,m);
sum %= m;
}
printf("%d\n",sum);
}
return 0;
} /*************************************************************/ #include<stdio.h>
#include<string.h>
int main()
{
long long z,h,m,a,b,ans;
scanf("%lld",&z);
while(z--)
{
int sum = 0;
scanf("%lld",&m);
scanf("%lld",&h);
while(h--)
{
scanf("%lld%lld",&a,&b);
ans = 1;
while(b)
{
if(b&1)
ans=ans*a%m;
a=a*a%m;
b>>=1;
}
sum += ans;
sum %= m;
}
printf("%d\n",sum%m);
}
return 0;
}
poj1995 Raising Modulo Numbers【高速幂】的更多相关文章
- POJ1995 Raising Modulo Numbers(快速幂)
POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...
- POJ1995:Raising Modulo Numbers(快速幂取余)
题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...
- POJ1995 Raising Modulo Numbers
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6373 Accepted: ...
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- ZOJ2150 Raising Modulo Numbers 快速幂
ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...
- POJ-1995 Raising Modulo Numbers---快速幂模板
题目链接: https://vjudge.net/problem/POJ-1995 题目大意: 求一堆ab的和模上m 思路: 直接上模板 #include<iostream> #inclu ...
- POJ 1995 Raising Modulo Numbers (快速幂)
题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...
- 【POJ - 1995】Raising Modulo Numbers(快速幂)
-->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1 B1 A2 B2 A3 B3 ......... AH ...
- Raising Modulo Numbers(POJ 1995 快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5934 Accepted: ...
随机推荐
- hibernate 注解配置<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/X
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 编程之路-client学习知识点纲要(Web/iOS/Android/WP)
Advanced:高级内容 Architect:架构设计 Core:框架底层原理分析 Language:框架经常使用语言 Objective-C Dart Swift Java Network:网络 ...
- 基于am3358的led跑马灯測试
#include <sys/ioctl.h> #include<stdio.h> #include <fcntl.h> #include <sys/types ...
- 关于mybatis中,批量增删改查以及參数传递的问题
1.參数传递的问题 大多数情况下,我们都是利用map作为參数,而且大部分情况下都是仅仅有一个參数. 可是,我们也能够利用@param注解,来传入多个參数,此时,mybatis会自己主动将參数封装成ma ...
- Android界面相关的类
Android界面相关的类 Window Activity的显示界面对象,并作为顶层View被加入到WindowManager中.Window提供了标准的UI显示策略:界面背景.标题区域.默认的事件处 ...
- [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js
You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...
- js静态私有变量(将方法变成原型模式,被所有实例共享,而方法操作变量,故变量是静态)
js静态私有变量(将方法变成原型模式,被所有实例共享,而方法操作变量,故变量是静态) 一.总结 1.js函数中的private和public:js函数中的私有变量 var 变量名,公有变量 this. ...
- 【Codeforces Round #185 (Div. 2) D】Cats Transport
[链接] 链接 [题意] 有n座山,m只猫. 每只猫都在其中的一些山上玩. 第i只猫在h[i]山上玩,且会在t[i]时刻出现在山脚下(然后就一直在那里等) 然后有p个人. 它们听从你的安排. 在某个时 ...
- [React] Setup 'beforeunload' listener
In this lesson we'll show how to take a beforeUnload call and convert it to a declarative React Comp ...
- WIFI 状态栏显示的wifi信号强度与wifisetting列表不一致
[DESCRIPTION] 状态栏显示的wifi信号强度与wifisetting列表不一致(不同步) [ANALYSIS] 1.apk都是接收RSSI_CHANGED intent,并调用WifiMa ...