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. Git 克隆指定分支代码

    git clone 指定分支 拉代码 1.git clone 不指定分支 git clone  http://10.1.1.11/service/sz-service.git 2.git clone ...

  2. poj2230 欧拉回路

    http://poj.org/problem?id=2230 Description Bessie's been appointed the new watch-cow for the farm. E ...

  3. Spring中的设计模式--观察者模式

    spring在容器中使用了观察者模式: 一.spring事件:ApplicationEvent,该抽象类继承了EventObject类,jdk建议所有的事件都应该继承自EventObject. 二.s ...

  4. 封装一个ExcelHelper,方便将Excel直接转成Datatable对象

    public class ExcelHelper { /// <summary> /// Excel转换成DataTable /// </summary> /// <pa ...

  5. javascript-es6学习笔记

    es6技术培训文档 第一阶段:1.let与const用法2.变量的解构赋值3.字符串的扩展4.正则的扩展5.数组的扩展6.函数的扩展7.对象的扩展8.Symbol9.Set和Map数据结构 第二阶段: ...

  6. 环境变量 - Maven

    Linux 1. 备份并编辑配置文件 # cp /etc/profile /etc/profile.bak # vi /etc/profile 2. 设置Maven环境变量 export MAVEN_ ...

  7. FJWC 2019 游记

    FJWC 2019 游记 Day 0 春节旅游, 刚从杭州绍兴一带赶回来, 然而并没有直接飞去福州, 去了厦门再去福州, 浪费了好多时间. Day 1 酒店到学校有 \(20\) 分钟的步行路程, 感 ...

  8. 数据库学习(二) case when then else end 的使用

    case函数还用来统计数据的,参考资料:https://www.cnblogs.com/qlqwjy/p/7476533.html 这里我只是整理下工作中使用的到案例: 查询语句: SELECT t. ...

  9. LuffyCity-CMDB实战

    第1章 章节一 课时01-ITIL介绍 课时02-CMDB介绍 课时03-CMDB需求讨论 课时04-CMDB需求讨论2 课时05-CMDB表结构设计 课时06-CMDB表结构设计2 课时07-CMD ...

  10. resetroot_169route_python2(用于ubuntu12.04和14.04,centos系列)

    #!/usr/bin/python import os import json import subprocess from cloudinit.sources.DataSourceConfigDri ...