题目链接:

Segment

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

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≤10^18,1≤P≤10^18,1≤T≤10.

 
Output
 
    Output 1 number to each testcase,answer mod P.
 
Sample Input
 
1
2 107
 
Sample Output
 
0
 
 
思路
 
直线x+y=q,在第一象限形成的三角形内部有多少个整数点;
 
题意:
 
条件转化成x+y<q&&x>0&&y>0;
可以发现当x取1到q-1时y正好取q-2到0,所以和就是(q-1)*(q-2)/2;
 
但是q和p都是<=1e18,所以算(q-1)*(q-2)/2可以大数乘法然后取模,或者用俄罗斯乘法算这两个数的积;
俄罗斯乘法的方法与快速幂的方法类似;
 
AC代码
 
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const int mod=1e9+;
ll p,q;
void solve(ll a,ll b)
{
ll s=,base=a;
while(b)
{
if(b&)
{
s+=base;
s%=q;
b--;
}
else
{
base*=;
base%=q;
b=(b>>);
}
}
printf("%lld\n",s);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&p,&q);
if(p==)
{
printf("0\n");
}
else
{
ll x=(p-)/%q,y=(p-)%q;
solve(x,y);
}
}
return ;
}
 

hdu-5666 Segment(俄罗斯乘法or大数乘法取模)的更多相关文章

  1. hdu 5666 Segment 俄罗斯乘法或者套大数板子

    Segment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  2. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  3. Divide two numbers,两数相除求商,不能用乘法,除法,取模运算

    问题描述:求商,不能用乘法,除法,取模运算. 算法思路:不能用除法,那只能用减法,但是用减法,超时.可以用位移运算,每次除数左移,相当于2倍. public class DividTwoInteger ...

  4. hdu 3037 费马小定理+逆元除法取模+Lucas定理

    组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...

  5. E - A^B mod C (大数乘方取模)

    Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63) ...

  6. HDU 5666 Segment 数论+大数

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

  7. hdu 4762 Cut the Cake (大数乘法)

    猜公式: ans=n/m^(n-1) #include<stdio.h> #include<string.h> struct BigNum { ]; int len; }; i ...

  8. HDU 5666 Segment

    Segment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. HDU-4704 Sum 大数幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704 题意:求a^n%m的结果,其中n为大数. S(1)+S(2)+...+S(N)等于2^(n-1) ...

随机推荐

  1. scala工具sbt的安装和使用;idea如何创建scala项目

    scala的sbt类似于java的maven mac:brew install sbt linux:yum Install sbt 或者下载二机制包 使用sbt需要想mvn一样搭建公司私服,不然,下载 ...

  2. Celery 启动报错 can_read() got an unexpected keyword argument timeout

    问题: Celery 启动报错 can_read() got an unexpected keyword argument timeout 方案:更改redis版本和celery版本,我使用下面的ce ...

  3. 权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现

    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现 import java.math.BigInteger; import java.util.ArrayLi ...

  4. TI C66x DSP 四种内存保护问题 -之- CPU訪问corePac内部资源时的内存保护问题

    CPU訪问corePac内部资源(L1.L2)时的内存保护(通过设置内存的訪问权限实现)等问题请參考以下两个blog.已经叙述的非常具体. "TI C66x DSP 系统events及其应用 ...

  5. GG链路过多port不足导致的报错OGG-01223

    假设我们GG同步链路在增多.就有可能出现这个报错.在日志中能体现. 2014-05-20 13:32:38 WARNING OGG-01223 TCP/IP error 111 (Connection ...

  6. MySQL windows集群(转)

    http://blog.csdn.net/zhangking/article/details/5670070    MySQL 群集是 MySQL 适合于分布式计算环境的高可用.高冗余版本.它采用了 ...

  7. HttpUtility.UrlEncode,Request.RawUrl,HttpUtility.UrlDecode,HttpUtility.UrlPathEncode,Uri.EscapeDataString

    碰到同样问题, 记录一下. 引自:https://www.cnblogs.com/ken-admin/p/5826480.html HttpUtility.UrlDecode(url),从Encode ...

  8. JavaScript包管理器综述

    JavaScript包管理器综述 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 对于JavaScript来说.包管理器 ...

  9. socketserver模块的使用

    import socketserver class MyTCPhandler(socketserver.BaseRequestHandler): def handle(self): # print(s ...

  10. 已迁移到http://www.coffin5257.com

    点我直达