zoukankan      html  css  js  c++  java
  • HDU 5666 快速乘

    Segment

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 360    Accepted Submission(s): 134


    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
     
    题意:判断直线x+y=q与坐标轴围成的三角形“内”的整数点的个数 很容易推出公式  (q-1)*(q-2)/2
     
    题解: 直接乘会爆long long 
             快速乘算法 处理
             类比快速幂模拟
    http://www.2cto.com/kf/201505/396902.html
    ll kuai(ll q,ll num,ll mod){
        ll ans=0;
        ll base=q;
        while(num){
            if(num%2) ans=(ans+base)%mod;
            num/=2;
            base=(base+base)%mod;
        }
        return ans%mod;
    }
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #define ll __int64
     5 using namespace std;
     6 int t;
     7 ll q,p;
     8 ll ans1,ans2;
     9 ll re;
    10 ll kuai(ll q,ll num,ll mod){
    11     ll ans=0;
    12     ll base=q;
    13     while(num){
    14         if(num%2) ans=(ans+base)%mod;
    15         num/=2;
    16         base=(base+base)%mod;
    17     }
    18     return ans%mod;
    19 }
    20 int main()
    21 {
    22     while(scanf("%d",&t)!=EOF)
    23     {
    24         for(int i=1;i<=t;i++)
    25         {
    26             scanf("%I64d %I64d",&q,&p);
    27             if(q%2==0)
    28             {
    29                 ans1=(q-2)/2;
    30                 ans1=ans1%p;
    31                 ans2=q-1;
    32             }
    33             else
    34             {
    35                 ans1=(q-1)/2;
    36                 ans1=ans1%p;
    37                 ans2=q-2;
    38             }
    39           printf("%I64d
    ",kuai(ans1,ans2,p));
    40         }
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    C/C++
    不使用判断语句求一组数中的奇数/偶数个数
    heap(堆)
    One-Hot Encoding(独热编码)
    泰坦尼克号生存预测
    LaTeX 使用笔记
    python学习 —— seaborn、matplotlib、pandas、numpy package的混合使用
    Python 读取本地*.txt文件 替换 内容 并保存
    MySQL基本命令脚本
    MySQL简介
  • 原文地址:https://www.cnblogs.com/hsd-/p/5399830.html
Copyright © 2011-2022 走看看