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 ;
}

HDU 5666 快速乘的更多相关文章

  1. HDOJ 5666//快速积,推公式

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5666 题意:给一条直线x+y=q,在(0,0)往x+y=q上面的整数点连线,x+y=q与x,y轴截成的三角 ...

  2. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  3. HDU 5666 Segment 数论+大数

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5666 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  4. hdu 5666 (大数乘法) Segment

    题目:这里 题意:在线段x+y=q与坐标轴围成的三角形中,求有多少个坐标为整数的点,答案模上p. 很容易就想到最后答案就是((q-1)*(q-2))/2然后模上p就是了,但是这个数字比较大,相乘会爆l ...

  5. hdu 1061 快速幂

    求n^n的个位 Sample Input 2 3 4 Sample Output 7 6 直接快速幂了,注意要用long long #include<cstdio> long long q ...

  6. hdu 2814 快速求欧拉函数

    /** 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 思路: 快速求欧拉函数 **/ #include <iostream> #include & ...

  7. C - Rikka with Badminton --- HDU 6425 快速幂加组合数学

    ---恢复内容开始--- 题目链接: https://vjudge.net/problem/1812693/origin 这题的mod运算很恶心,真的... 本题有正反两个思路,一个是正面求解其不能成 ...

  8. hdu 5187 快速幂 + 快速乘 值得学习

    就是以那个ai为分水岭,左边和右边都分别是单调增或单调减如图         就这四种情况,其中头两种总共就是两个序列,也就是从头到尾递增和从头到尾递减.         后两种方式就是把序列中德数分 ...

  9. hdu 1852(快速幂模+有除法的时候取模的公式)

    Beijing 2008 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Tota ...

随机推荐

  1. python循环,函数

    平常的网页会转换成ascll码,在转编译成十六进制a='http://www.mywebsit.com/?query=python&count=20'print(a)b=a.replace(' ...

  2. python七类之字符串

    字符串 一.关键字:str 字符串是不可变的可迭代的数据类型 二.方法: name = 'alex uwu sir' .title #标题 使首字母大写​​ 只要有特殊字符隔开,才能分别认为是多个单词 ...

  3. C++11中initializer lists的使用

    Before C++11,there was no easy way to do things like initialize a std::vector or std::map(or a custo ...

  4. 3124: [Sdoi2013]直径

    3124: [Sdoi2013]直径 https://www.lydsy.com/JudgeOnline/problem.php?id=3124 分析: 所有直径都经过的边,一定都是连续的一段.(画个 ...

  5. 关于实现mybatis order by 排序传递参数实现 问题记录

    一    问题场景:本人项目纯纯的后端系统  并且项目前端采用纯纯的原生js 实现 1)表格  通过查询列表数据放入到域中  前段采用 for循环的方式实现遍历生成列表 2)分页实现本人是公司内部自定 ...

  6. 使用apache的ab压力测试时失败请求原因

    只要出现 Failed requests 就会多出现一行要求失败的各原因的数据统计,分别有 Connect, Length,与 Exception 三种,分别代表的意义为:Connect      无 ...

  7. 关闭 Identity 插入限制

    当为identity列插入时会报错: 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'xx'中的标识列指定显式值. 但在复制表数据时想带主键一起复制时,这时要设置IDEN ...

  8. 『Golang』在Golang中使用json

    由于要开发一个小型的web应用,而web应用大部分都会使用json作为数据传输的格式,所以有了这篇文章. 包引用 import ( "encoding/json" "gi ...

  9. 「暑期训练」「基础DP」 Common Subsequence (POJ-1458)

    题意与分析 很简单:求最长公共子序列. 注意子序列与子串的差别:一个不连续一个连续.一份比较好的参考资料见:https://segmentfault.com/a/1190000002641054 状态 ...

  10. Ruby中数组的&操作

    最近在忙一个项目,好久没有写日志了,项目终于接近尾声,可以适当放松一下,所以记一下在这个项目中发现的有趣事情: 数组的 与 操作 一直以为两个数组A和B相与,谁前谁后都一样,不过这次在项目中突然想试一 ...