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

本文介绍了一种计算特定条件下三角形内部及其边界上整数坐标点数量的方法,并提供了两种实现方式,一种使用C++结合俄罗斯乘法,另一种使用Java大数处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 2q1018,1P1018,1T10.
 

 

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 = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
ll eluosimul(ll x,ll y,ll mod)
{
    ll sum=0;
    while(x)
    {
        if(x&1)
        {
            sum+=y;
            sum%=mod;
        }
        x>>=1;
        y*=2;
        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%2)
        ans=eluosimul(q-2,(q-1)/2,m);
        else
        ans=eluosimul(q-1,(q-2)/2,m);
        printf("%I64d\n",ans);
    }
    return 0;
}
View Code

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!=0)
        {
            x--;
            BigInteger c=new BigInteger("2");
            BigInteger e=new BigInteger("1");
            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));
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/jhz033/p/5400453.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值