HDU 5666 快速乘
2016-04-16 23:36
375 查看
Segment
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 360 Accepted Submission(s): 134
[align=left]Problem Description[/align]
Silen August does not like to talk with others.She like to find some interesting problems.
Today she finds an interesting problem.She finds a segment x+y=q
.The segment intersect the axis and produce a delta.She links some line between (0,0)
and the node on the segment whose coordinate are integers.
Please calculate how many nodes are in the delta and not on the segments,output answer mod P.
[align=left]Input[/align]
First line has a number,T,means testcase number.
Then,each line has two integers q,P.
q
is a prime number,and 2≤q≤1018,1≤P≤1018,1≤T≤10.
[align=left]Output[/align]
Output 1 number to each testcase,answer mod P.
[align=left]Sample Input[/align]
1
2 107
[align=left]Sample Output[/align]
0
[align=left]Source[/align]
BestCoder Round #80
题意:判断直线x+y=q与坐标轴围成的三角形“内”的整数点的个数 很容易推出公式 (q-1)*(q-2)/2
题解: 直接乘会爆long long
快速乘算法 处理
类比快速幂模拟 http://www.2cto.com/kf/201505/396902.html ll kuai(ll q,ll num,ll mod){
ll ans=0;
ll base=q;
while(num){
if(num%2) ans=(ans+base)%mod;
num/=2;
base=(base+base)%mod;
}
return ans%mod;
}
#include<iostream> #include<cstring> #include<cstdio> #define ll __int64 using namespace std; int t; ll q,p; ll ans1,ans2; ll re; ll kuai(ll q,ll num,ll mod){ ll ans=0; ll base=q; while(num){ if(num%2) ans=(ans+base)%mod; num/=2; base=(base+base)%mod; } return ans%mod; } int main() { while(scanf("%d",&t)!=EOF) { for(int i=1;i<=t;i++) { scanf("%I64d %I64d",&q,&p); if(q%2==0) { ans1=(q-2)/2; ans1=ans1%p; ans2=q-1; } else { ans1=(q-1)/2; ans1=ans1%p; ans2=q-2; } printf("%I64d\n",kuai(ans1,ans2,p)); } } return 0; }
相关文章推荐
- leetcode 16: 3 Sum closest (C#版)
- MFC CStatic类动态创建
- VLC播放RTSP视频延迟问题
- 利用链表实现多项式求值
- VS2013中 scanf()函数返回值
- Jersey(1.19.1) - Security
- Linux防止“rm -rf /”误操作的方法
- Merge OUTPUT 高级用法综合写的一个MergeTab的存储过程
- POJ-1655 Balancing Act
- 数据平台的简单使用
- SLAM学习
- Django:之ORM、CMS和二维码生成
- Windows server 2012R2安装活动目录
- 方便学习的一些经典网站
- HDU 5665
- 使用 PDO 方式将 Session 保存到 MySQL 数据中
- 抛砖微信公众号应用开发
- mysql 下载及安装方法
- 【ATF】钱正平:大规模实时计算及其在阿里的应用与创新
- ubuntu下使用VNC连接树莓派raspberry