2018航电多校练习第9场-快速幂

Rikka with Badminton

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 53    Accepted Submission(s): 44

Problem Description

In the last semester, Rikka joined the badminton club.

There are n students in the badminton club, some of them have rackets, and some of them have balls. Formally, there are a students have neither rackets nor balls, bstudents have only rackets, c students have only balls, and d students have both rackets and balls. (a+b+c+d=n)

This week, the club is going to organize students to play badminton. Each student can choose to take part in or not freely. So there are 2n possible registration status.

To play badminton, there must be at least two students who have rackets and at least one students who have balls. So if there aren't enough balls or rackets, the activity will fail.

Now, Rikka wants to calculate the number of the status among all 2n possible registration status which will make the activity fail.

Input

The first line contains a single number t(1≤t≤103), the number of testcases.

For each testcase, the first line contains four integers a,b,c,d(0≤a,b,c,d≤107,a+b+c+d≥1).

Output

For each testcase, output a single line with a single integer, the answer modulo 998244353.

Sample Input

3

1 1 1 1

2 2 2 2

3 4 5 6

Sample Output

12

84

2904

题意:

给你a,b,c,d四种人的人数:分别代表什么都没有,一个拍,一个球,一个球一个拍

问你有多少种可能连一个球两个拍都没有

a不需要讨论

当d取1时,b必须为0,c随便取:(fun(2,a)%maxx * fun(2,c)%maxx) *d %maxx;

当d取0时,b取1或0,c随便取:(fun(2,a)%maxx * (b+1)%maxx)*fun(2,c)%maxx;

当d取0时,b取0或1以外的数,c必须为0:(fun(2,a)%maxx * (fun(2,b)-b-1+maxx)%maxx)%maxx;

三种情况相加,输出时还要取模

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define maxx 998244353
 5
 6 ll fun(ll a,ll b){
 7 ll r = 1;
 8 while(b){
 9         if(b&1){
10                 r = (r*a)%maxx;
11         }
12         a = (a*a)%maxx;
13         b>>=1;
14 }
15 return r;
16 }
17 int main(){
18 int T;
19 while(scanf("%d",&T)!=EOF){
20       while(T--){
21             ll a,b,c,d;
22             scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
23             ll ans = 0;
24             ans += (fun(2,a)%maxx * fun(2,c)%maxx) *d %maxx;
25             ans += (fun(2,a)%maxx * (b+1)%maxx)*fun(2,c)%maxx;
26             ans += (fun(2,a)%maxx * (fun(2,b)-b-1+maxx)%maxx)%maxx;
27             printf("%lld\n",ans%maxx);
28       }
29 }
30 return 0;
31 }

乘法取模:

(a*b)%mod = ((a%mod) * (b*mod)) % mod

除法取模:

费马小定理:若p是质数,且a、p互质,那么a^(p-1) mod p = 1。
现在,我们要求a/c mod p,通过一系列神奇的转化,那万恶的除法就会神奇地消失...
a / c mod p
= a / c mod p * 1
= a / c mod p * c^(p-1) mod p
= a * c^(p-2) mod p

快速幂:

乘法在计算机中处理的时间并不是这么快的,也要拆分为加法来做的。所以快速乘法会更快的计算a*b的结果,而且a*b%mod可能还没取模就已经爆long long,但快速乘法却不会。快速幂也是同样的道理。
实现的原理都是基于按照二进制位一步一步乘来避免重复的操作,利用前面的中间结果,从而实现快速的目的。
对于乘数b来说,势必可以拆成2进制,比如110101。有一些位为0,有一些位为1。根据乘法分配律:a*b=a*(b1+b2+b3+……) 那么对于a*53 = a*110101(二进制)= a*(100000+10000+100+1)=a*(100000*1+10000*1+1000*0+100*1+10*0+1*1)。 那么设立一个ans=0用于保存答案,每一位让a*=2,在根据b的对应为1看是不是加上此时的a,即可完成快速运算。

long long q_mul( long long a, long long b, long long mod ) //快速计算 (a*b) % mod
{long long ans = 0;           // 初始化while(b)                     //根据b的每一位看加不加当前a
    {if(b & 1)                //如果当前位为1
        {b--;ans =(ans+ a)%mod;   //ans+=a
        }b >>= 1;                 //b向前移位a = (a + a) % mod;       //更新a
 }return ans;
}

long long q_pow( long long a, long long b, long long mod ) //快速计算 (a^b) % mod
{long long ans = 1;                       // 初始化while(b)                                 //根据b的每一位看乘不乘当前a
    {if(b & 1)                            //如果当前位为1
        {ans = (ans*a)%mod                //ans*=a
        }b >>= 1;                             //b向前移位a = (a*a)%mod;                       //更新a
    }return ans;
}

posted @ 2018-08-20 23:27 M_x_j 阅读(...) 评论(...) 编辑 收藏

2018航电多校练习第9场-快速幂相关推荐

  1. 2022杭电多校赛第八场

    2022杭电多校赛第八场 文章目录 2022杭电多校赛第八场 1004.Quel'Thalas 1001.Theramore 1011.Stormwind 1008.Orgrimmar 1005.Ir ...

  2. 2021杭电多校补题——第一场

    2021杭电多校补题--第一场 文章目录 Mod, Or and Everything Rocket land(待补) Puzzle loop(待补) Another thief in a Shop( ...

  3. 2018杭电多校第二场1006(容斥原理,组合数学)

    有能力时再回来解决吧= ̄ω ̄= 转载于:https://www.cnblogs.com/ldudxy/p/9521721.html

  4. 航电多校第二场-1001-Total Eclipse

    题目链接 题目大意 n个点的图,每个点有点权,每次选取一个最大联通分量,然后其中每个点点权-1,点权不能为负,求最少需要操作的次数 题解 按照点权对所有点进行排序,然后依次加边建图,进行反向并查集.先 ...

  5. HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场

    题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...

  6. 2022杭电多校联赛第八场 题解

    比赛传送门 作者: fn 目录 签到题 1004题 Quel'Thalas / 奎尔萨拉斯(幻方) 1001题 Theramore / 塞拉摩岛(翻转字符串) 基本题 1011题 Stormwind ...

  7. 杭电多校2020 第十场 C - Mine Sweeper HDU - 6879

    题目大意:(来源) t组输入,每组输入一个s 你需要输出一个r行c列的阵列,这个阵列中'X'代表炸弹,'.'表示没有炸弹 对于'.'这些位置都会有一个数值,这个值取决于这个位置附近8个位置,这8个位置 ...

  8. 2018.11.08 NOIP模拟 景点(倍增+矩阵快速幂优化dp)

    传送门 首先按照题意构造出转移矩阵. 然后可以矩阵快速幂求出答案. 但是直接做是O(n3qlogm)O(n^3qlogm)O(n3qlogm)的会TTT掉. 观察要求的东西发现我们只关系一行的答案. ...

  9. 便携式航电实时系统测试平台产品介绍

    产品介绍 系统概述 便携式航电实时系统测试平台由测试硬件子系统和设备级应用软件组成,设备级应用软件又划分为测试设计子系统.测试运行子系统.测试监控与分析子系统.如图1所示. 1 便携式航电实时系统测试 ...

最新文章

  1. 温控自动烘焙系统的研究与实现
  2. Windows Phone 几种页面间传递数据的方式
  3. Python实现RGB和Lab颜色空间互转
  4. svn 不支持http 客户端_Xversion for mac(SVN客户端)
  5. Nginx服务的信号控制之USR2
  6. 使用HeartBeat实现高可用HA的配置过程详解
  7. ef mysql 读写分离_基于 EntityFramework 的数据库主从读写分离服务插件
  8. java亚马逊模拟登录_java – 亚马逊MWS入门
  9. Spring - 理解BeanPostProcessor
  10. 【瑕疵检测】基于matlab GUI OTSU织物疵点检测【含Matlab源码 860期】
  11. 渗透测试实验_安装Windows 2003 企业版
  12. 含不等式的优化设计matlab,基于Matlab的齿轮传动优化设计
  13. IIS配置webp后缀文件
  14. 如何用VBA从EXCEL表取数据?问题1:1次性整体写入,还是循环写入数组呢? 问题2:取得数据后如何定位需要的那个?
  15. 数通--交互技术--STP+RSTP详解
  16. 商城系统春节氛围营造
  17. 虹科教您 | KPA Automation softPLC入门操作指南(1)
  18. Usb 声卡 linux,树莓派:使用usb声卡播放音乐
  19. 咩小饬的造轮子之路(彩票篇)
  20. IFR报告显示过去五年全球工业机器人销量翻番

热门文章

  1. 【数据分析】贝叶斯原理
  2. python进程池multiprocessing.Pool和线程池multiprocessing.dummy.Pool实例
  3. Android4.4监听耳机插入处理方法
  4. 【数据库运维】mysql备份恢复练习
  5. seek 方法java,Seek.java
  6. python 函数式编程包_Python——详解函数式编程与闭包
  7. Unity3D-游戏场景优化之遮挡剔除(Occlusion Culling)的使用
  8. 机器学习(三):线性模型
  9. 如何使用gcore以及viewcore排查问题
  10. 算法设计与分析基础知识点