zoukankan      html  css  js  c++  java
  • hdu-5666 Segment(俄罗斯乘法or大数乘法取模)

    题目链接:

    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 2q10^18,1P10^18,1T10.
     
    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+5;
    typedef long long ll;
    const int mod=1e9+7;
    ll p,q;
    void solve(ll a,ll b)
    {
        ll s=0,base=a;
        while(b)
        {
            if(b&1)
            {
                s+=base;
                s%=q;
                b--;
            }
            else
            {
                base*=2;
                base%=q;
                b=(b>>1);
            }
        }
        printf("%lld
    ",s);
    }
    int main()
    {
       int t;
       scanf("%d",&t);
       while(t--)
       {
           scanf("%lld%lld",&p,&q);
           if(p==2)
           {
               printf("0
    ");
           }
           else
           {
               ll x=(p-1)/2%q,y=(p-2)%q;
               solve(x,y);
           }
       }
        return 0;
    }
     
  • 相关阅读:
    学习视频收集
    vscode 编译器插件
    vue2.0父子组件之间传值
    js 案例
    插件
    【转】30分钟掌握 C#6
    【初码干货】关于.NET玩爬虫这些事
    上机作业七 系统进程与计划任务管理
    客户端与服务器双向密钥对验证
    DHCP中继配置
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5400857.html
Copyright © 2011-2022 走看看