By property of mod operations , we can simply use Divide and Conquer + Recursion to solve it. Reference: https://www.khanacademy.org/math/applied-math/cryptography/modarithmetic/a/modular-exponentiation

My Ruby version is:

DIV = 20

def ferma(x, y, n)
c = 1
for i in 0..y-1
c = (c * x) % n
end
#p "[#{x}^#{y} mod #{n} = #{c}]"
return c
end def div_conq_ferma(x, y, n)
#p "#{x}^#{y} mod #{n} = (#{x}^#{DIV})^#{y/DIV} * #{x}^#{y%DIV}, mod #{n}" mod1 = ferma(x, y % DIV, n) if (y > DIV)
sub_mod0 = ferma(x, DIV, n)
pwr_sub_mod0 = y / DIV
mod0 = div_conq_ferma(sub_mod0, pwr_sub_mod0, n)
else
mod0 = 1
end return ferma(mod1 * mod0, 1, n)
end
#
runcnt = gets.to_i
for i in 0..runcnt-1
str = gets.split
x = str[0].to_i
y = str[1].to_i
n = str[2].to_i
p div_conq_ferma(x, y, n)
end

But seems Ruby is not fast enough to pass the second case. All Ruby submissions failed the 2nd case with TLE.

SPOJ #752. Power it!的更多相关文章

  1. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  2. 「SPOJ 3105」Power Modulo Inverted

    「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...

  3. [BZOJ2616]SPOJ PERIODNI 树形dp+组合数+逆元

    2616: SPOJ PERIODNI Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 128  Solved: 48[Submit][Status][ ...

  4. SPOJ AMR12B 720

    这个题应该是个优先队列的模版题 当时比赛的时候没时间做先贴一下大神的代码好好学习学习 B - Gandalf vs the Balrog Time Limit:2000MS     Memory Li ...

  5. 一起学微软Power BI系列-使用技巧(5)自定义PowerBI时间日期表

    1.日期函数表作用 经常使用Excel或者PowerBI,Power Pivot做报表,时间日期是一个重要的纬度,加上做一些钻取,时间日期函数表不可避免.所以今天就给大家分享一个自定义的做日期表的方法 ...

  6. 一起学微软Power BI系列-使用技巧(4)Power BI中国版企业环境搭建和帐号问题

    千呼万唤的Power BI中国版终于落地了,相信12月初的微软技术大会之后已经铺天盖地的新闻出现了,不错,Power BI中国版真的来了,但还有些遗憾,国际版的一些重量级服务如power bi emb ...

  7. 一起学微软Power BI系列-使用技巧(3)Power BI安卓手机版安装与体验

    Power BI有手机版,目前支持安卓,苹果和WP,不过没有WP手机,苹果在国内还不能用,要FQ和用就不测试了.安卓的我也也是费了九牛二虎之力才把app下载下来,把方法分享给大家. FQ太麻烦,所以建 ...

  8. Power BI官方视频(3) Power BI Desktop 8月份更新功能概述

    Power BI Desktop 8月24日发布了更新版本.现将更新内容翻译整理如下,可以根据后面提供的链接下载最新版本使用. 1.主要功能更新 1.1 数据钻取支持在线版 以前的desktop中进行 ...

  9. 一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库

    说起Oracle数据库,以前没用过Oracle不知道,但是这1年用Oracle后,发现真的是想狂吐槽,特别是那个.NET驱动和链接字符串,特别奇葩.总归是和其他数据库不一样,标新立异,不知道为何.另外 ...

随机推荐

  1. css 包含的图片和style="display:none"可以避免图片加载,可以节省网络流量

    从别人那儿学到一招:先记录下来: <head> <meta charset="UTF-8"> <title>Document</title ...

  2. shell命令:删除当前.sh文件所在目录下的zip包,并且重新打包

    filepath=$(cd ")"; pwd) packagePath="$filepath"/package zipPath="$filepath& ...

  3. Linux Command Line 备忘

    1. 如果要删除目录, rmdir or rm -d 或许可以删除空目录,但是只有 rm -R 可以把目录以及其内容连带删除! 2. 查看文件大小: ls -l --block-size=G 还可以换 ...

  4. hive-jdbc/odbc的解读和看法

    当前的项目中, 涉及到了hive-jdbc/odbc这块, 因此把这几天所调研的资料作一份总结.本文讲解hive-jdbc/hive-odbc的实现, 以期对jdbc/odbc规范和实现有个较深入的理 ...

  5. TextView所有属性

    android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all) android: ...

  6. indexOf()的用法

    indexOf()的用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判断和截取字符串! indexOf()定义和用法indexOf( ...

  7. 关于获取目录的N种方法 的汇总

    前段时间在Global.asax.cs中的Session_End中使用Server.MapPath() 出现"服务器操作在此上下文中不可用"异常. 网络上给出的解决方案:Syste ...

  8. Codeforces Round #112 (Div. 2)

    Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...

  9. spark之数据源之自动分区推断

    在hadoop上创建目录/spark-study/users/gender=male/country=US/users.parquet(并且把文件put上去) code: package cn.spa ...

  10. 【转】iOS开发拓展篇—静态库

    原文网址:http://www.cnblogs.com/wendingding/p/3893095.html iOS开发拓展篇-静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码 ...