题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M.

思路:快速幂

实例

3^11  11=2^0+2^1+2^3    =》 3^1*3^2*3^8=3^11

实现代码:

int solve(int a,int b)

{ int ans=1;    

     while(b){ if(b&1)  ans=ans*a;   a=a*a;  b>>=1;}

}

解释一下代码:b&1即二进制表达式的最后一位,  11二进制为 1011 b>>=1 去掉二进制的最后一位

模的运算  (a^b)%p=(a%p)^b%p

解决问题的代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
ll mod_m(ll a, ll b, int p)
{
ll ans = 1ll;
a %= p;
while (b)
{
if (b & )
ans = (ans*a) % p;
a = (a*a) % p;
b >>= ;
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
ll ans = 0ll;
int m;
scanf("%d", &m);
int h;
scanf("%d", &h);
while (h--)
{
ll a, b;
scanf("%lld%lld", &a, &b);
ans += mod_m(a, b, m);
}
printf("%lld\n", (ans%m));
}
return ;
}

poj 1995 快速幂的更多相关文章

  1. POJ 1995 快速幂模板

    http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include< ...

  2. Raising Modulo Numbers(POJ 1995 快速幂)

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5934   Accepted: ...

  3. POJ 1995 (快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  4. POJ 1845-Sumdiv(快速幂取模+整数唯一分解定理+约数和公式+同余模公式)

    Sumdiv Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  5. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

  6. POJ 3641 快速幂+素数

    http://poj.org/problem?id=3641 练手用,结果念题不清,以为是奇偶数WA了一发 #include<iostream> #include<cstdio> ...

  7. Pseudoprime numbers(POJ 3641 快速幂)

    #include <cstring> #include <cstdio> #include <iostream> #include <cmath> #i ...

  8. poj 3641 快速幂

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  9. POJ 1995 Raising Modulo Numbers(快速幂)

    嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...

随机推荐

  1. python学习《一》

    从词篇博客开始,记录python3.0学习笔记 python3里    字符串用双引号,或者单引号,包起来 3双引号和3单引号   代表换行

  2. 故障案例:主从同步报错Fatal error: The slave I/O thread stops because master and slave have equal MySQL server

    https://blog.csdn.net/cug_jiang126com/article/details/46846031

  3. 【0 基础学Dojo】第【1】篇 HelloWord

    打开dojo 官网首页 http://dojotoolkit.org/,我们看到 点击get dojo  你将得到下载Dojo 的不同方式 2,点击下面方式下载, 解压后 新建myTest.html, ...

  4. WPF (VisualChildren)可视化子元素详解

    VisualChildrenCount    的 FrameworkElement 实现始终返回 0 或 1.  如果类所要维护的可视化子元素集合的成员数可能超过 1,则这样的类必须重写此属性和 Ge ...

  5. 事务的隔离级别和mysql事务隔离级别修改

    A事务做了操作 没有提交 对B事务来说 就等于没做 获取的都是之前的数据 但是 在A事务中查询的话 查到的都是操作之后的数据 没有提交的数据只有自己看得到,并没有update到数据库. 查看InnoD ...

  6. ribbon hystrix仪表盘

    Circuit Breaker: Hystrix Dashboard (断路器:hystrix 仪表盘) 基于service-ribbon 改造下: pom.xml加入: <dependency ...

  7. Git入门学习总结

    用了两天时间看完廖雪峰老师的git教程(http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...

  8. zabbix2升级zabbix3

    1.停服zabbix2,停服数据库/etc/init.d/zabbix_server stop/etc/init.d/mysqld stop 2.物理备份数据库cd /var/lib/mysql/ta ...

  9. 本号讯 | 微软和百度携手推进全球自动驾驶技术; 微软发布新一代可垂直可水平滚动的Arc鼠标

    7 月 13 日,微软宣布了与宝马的最新合作进展,继语音助手 Cortana .云服务 Azure.Office 365 和微软 Exchange 安装在部分宝马车型后——Skype for Busi ...

  10. MovieReview—NINE LIVES(九条命)

    Struggle & Family         A successful middle-aged man in the movie became a cat by falling from ...