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;
    }
     
  • 相关阅读:
    Spring
    sikuli常用方法学习
    运行测试Caused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path错误解决
    sikuli+java实例
    sikuli运行出现问题:Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform
    官网下载jdk
    java:jdk环境变量配置+tomcat环境变量配置
    Redis能干啥?细看11种Web应用场景
    计数场景的优化
    国内外三个领域巨头告诉你Redis怎么用
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5400857.html
Copyright © 2011-2022 走看看