Segment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 360    Accepted Submission(s): 134

Problem Description

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.

 
Input

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.

 
Output

Output 1 number to each testcase,answer mod P.

 
Sample Input
1
2 107
 
Sample Output
0
 
Source
 
题意:判断直线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=;
ll base=q;
while(num){
if(num%) ans=(ans+base)%mod;
num/=;
base=(base+base)%mod;
}
return ans%mod;
}
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=;i<=t;i++)
{
scanf("%I64d %I64d",&q,&p);
if(q%==)
{
ans1=(q-)/;
ans1=ans1%p;
ans2=q-;
}
else
{
ans1=(q-)/;
ans1=ans1%p;
ans2=q-;
}
printf("%I64d\n",kuai(ans1,ans2,p));
}
}
return ;
}

最新文章

  1. Java开发实践 集合框架 全面分析
  2. Struts2学习笔记--使用Response下载文件和Struts2的StreamResult文件下载
  3. 夺命雷公狗----Git---3---vi编辑器
  4. ActionSupport与action区别
  5. 用firebug给firefox添加信任链接
  6. Java 简单算法--打印乘法口诀(只使用一次循环)
  7. Objective c 自动释放池
  8. pktgen使用详细教程
  9. jdk7 中Collections.sort 异常
  10. Android SDCard Mount 流程分析
  11. 【Python篇】---Python3.5在Centoos的安装教程--超实用
  12. CPU监控
  13. Scrapy爬虫(5)爬取当当网图书畅销榜
  14. flask 请求上下文
  15. 使用强大的 Mockito 测试框架来测试你的代码
  16. TFS 之 彻底删除团队项目
  17. [脚本] 一个用于BMP到EPS转换的BAT脚本实现(需要安装bmeps)
  18. nginx反向代理部署与演示(二)
  19. ORA-03113:通信通道的文件结尾-完美解决方案
  20. 一款基于jQuery的图片分组切换焦点图插件

热门文章

  1. 二维数组快速排序(sort+qsort)
  2. python2.7练习小例子(七)
  3. js倒计时页面跳转
  4. 在WPF中创建可换肤的用户界面
  5. C# 获取当前日期当年的周数
  6. xamdin: 添加小组件报错: render() got an unexpected keyword argument &#39;renderer&#39;
  7. (原创)不过如此的 DFS 深度优先遍历
  8. gcc options选项的优化及选择
  9. [Binary Search] Leetcode 35, 74
  10. JavaScript 面向对象 原型(prototype) 继承