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≤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
题解:答案就是((p-1)*(p-2)/2)%mod;p是10^18以内直接乘就会爆;利用俄罗斯乘法的加法性质得到答案;(现场做只会想到java大数。。果然菜鸟。。。)
   俄罗斯乘法:http://baike.baidu.com/link?url=vVo1zdml29g80N-BYvpdm2hNGpYwSnGoJsnAJmook4AJBiYUVL_ort5f7XqFJ0yx6zxB5ha90q6-1LD6HxPIaa
俄罗斯乘法代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
ll eluosimul(ll x,ll y,ll mod)
{
ll sum=;
while(x)
{
if(x&)
{
sum+=y;
sum%=mod;
}
x>>=;
y*=;
y%=mod;
}
return sum;
}
int main()
{
ll x,y,z,i,t,m,q;
scanf("%I64d",&x);
while(x--)
{
ll ans;
scanf("%I64d%I64d",&q,&m);
if(q%)
ans=eluosimul(q-,(q-)/,m);
else
ans=eluosimul(q-,(q-)/,m);
printf("%I64d\n",ans);
}
return ;
}

java:

import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int x;
x=cin.nextInt();
while(x!=)
{
x--;
BigInteger c=new BigInteger("");
BigInteger e=new BigInteger("");
BigInteger a=cin.nextBigInteger();
BigInteger d=a.subtract(c);
BigInteger f=a.subtract(e);
BigInteger b=cin.nextBigInteger();
BigInteger ans=f.multiply(d);
ans=ans.divide(c);
System.out.println(ans.remainder(b));
}
}
}

hdu 5666 Segment 俄罗斯乘法或者套大数板子的更多相关文章

  1. hdu-5666 Segment(俄罗斯乘法or大数乘法取模)

    题目链接: Segment Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) P ...

  2. HDU 5666 Segment 数论+大数

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

  3. HDU 5666 Segment

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

  4. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  5. HDU 5372 Segment Game

    /** 多校联合2015-muti7-1004 <a target=_blank href="http://acm.hdu.edu.cn/showproblem.php?pid=537 ...

  6. C++大数板子

    C++大数板子 使用样例在主函数里看就好,必要的运算符都重载了. #include <iostream> using namespace std; ;/*精度位数,自行调整*/ //1.如 ...

  7. hdu 5666 (大数乘法) Segment

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

  8. HDU 1042 N!(高精度阶乘、大数乘法)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submi ...

  9. HDU 1402 A * B Problem Plus ——(大数乘法,FFT)

    因为刚学fft,想拿这题练练手,结果WA了个爽= =. 总结几点犯的错误: 1.要注意处理前导零的问题. 2.一定要注意数组大小的问题.(前一个fft的题因为没用到b数组,所以b就没管,这里使用了b数 ...

随机推荐

  1. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  2. 在ASP.NET Web Application中通过SOAP协议调用Bing搜索服务

    本文介绍了如何在ASP.NET Web Application中将Bing搜索作为Web Service来使用,并通过HTTP的SOAP协议在ASP.NET Web Application中调用Bin ...

  3. PostgreSQL远程连接配置

    postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的listen_addresses项值设定为“* ...

  4. 大神的博客地址liferay

    http://www.huqiwen.com/category/technology-share/liferay/

  5. Echarts使用及动态加载图表数据 折线图X轴数据动态加载

    Echarts简介 echarts,缩写来自Enterprise Charts,商业级数据图表,一个纯JavaScript的图表库,来自百度...我想应该够简洁了 使用Echarts 目前,就官网的文 ...

  6. LeetCode-MinimumDepthOfBinaryTree

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  7. GNU

    1983年,理查德.斯托曼提出GNU计划(革奴计划),希望发展出一套完整的开放源代码操作系统来取代Unix,计划中的操作系统,名为GNU. 1989年,发表GNU通用公共许可协议(GPL).GPL条款 ...

  8. Java互联网架构-Mysql分库分表订单生成系统实战分析

    概述 分库分表的必要性 首先我们来了解一下为什么要做分库分表.在我们的业务(web应用)中,关系型数据库本身比较容易成为系统性能瓶颈,单机存储容量.连接数.处理能力等都很有限,数据库本身的“有状态性” ...

  9. Linux基础命令---cmp

    cmp 用字节的方式,比较两个文件是否存在差异,但是不保存运算结果.Cmp指令只会根据结果设置相关的标志位,这个指令之后往往会跟着一个条件跳转指令. 此命令的适用范围:RedHat.RHEL.Ubun ...

  10. vm #set、日期截取、#foreach&#if

    <div class="form_row"> <label>状态:</label> #if ($!cp.runState =='t') #set ...