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;
    }
     
  • 相关阅读:
    Markdown 语法说明 (简体中文版)
    Markdown Reference
    BZOJ 2229 最小割
    BZOJ 3569 DZY Loves Chinese II
    BZOJ 3563 DZY Loves Chinese
    BZOJ 2956 模积和
    BZOJ 2957 楼房重建
    查漏补缺:面向对象设计原则
    添砖加瓦:设计模式(简单工厂模式)
    添砖加瓦:设计模式(总述)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5400857.html
Copyright © 2011-2022 走看看