首页 > 其他分享 >poj-3641

poj-3641

时间:2023-05-23 16:02:56浏览次数:35  
标签:power res long poj subRes include 3641 mod


//712K  0MS G++
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

long long a, p;

// long long power2(long long a, long long n)
// {
//     long long ret = 1;
//     for (long long m = a; n > 0; n >>= 1, m = m * m % p)
//         if (n & 1)
//             set = ret * m % p;
//     return ret;
// }

long long power(long long a, long long n) {
    if (n == 1) {
        return a%p;
    }
    long long  additional = 1;
    long long subRes = power(a, n/2);
    long long res = (subRes*subRes)%p;
    if (n & 1) {
        res = (res*a)%p;
    }
    return res;
}


bool prime(int a)
{
    for (int i = 2; i * i <= a; i++)
        if (a % i == 0)
            return false;
    return true;
}

int main()
{
    //freopen("t.txt", "r", stdin);
    while (scanf("%lld%lld", &p, &a), a | p)
    {
        long long x = power(a, p);
        // long long x2 = power2(a, p);
        // printf("COMPARE %lld %lld\n", x, x2);
        if (x == a && !prime(p))
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}



利用了次方的模运算来对大数进行压缩:

模运算满足分配率,(a*b)mod n = ((a mod n)*(b mod n)) mod n

那么对于N的x次方%M,

则可以不断的将N/2向下递归分解, power()是递归, power2()用的是循环(超别人的, 不是很清楚循环的写法),

注意的是,因为数值太大,因此任何两个大数相乘都要取模,在power函数中,一开始res = subRes*subRes时没有%p,造成了WA,

因此只要有两个大数乘,就都要再接着取一次模。

还是搞不清楚power的 递归 转 循环的写法,MARK下.

标签:power,res,long,poj,subRes,include,3641,mod
From: https://blog.51cto.com/u_9420214/6332978

相关文章

  • poj-1026
    //188K110MSC++#include<cstring>#include<cstdio>#include<iostream>usingnamespacestd;charstr1[205];charstr2[205];intkey[205];intcycleLength[205];//voidreplace(char*str,intkeyLength,intstrLength){//......
  • poj-2707
    //408K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;intoX;intoY;intdX;intdY;inlinedoubleMIN(doublea,doubleb){returna<b?a:b;}inlinedoubleMAX(doublea,doubleb){returna>b?......
  • poj-2635
    //1652K875MSG++1000//1648K1313MSG++10000#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>constintMAX=1000100;charnotPrime[MAX+1];intPrimeNum;intPrimes[MAX];voidcheckPrim......
  • poj-3286
    //stupidmethod!!!!!!!!!!!!!!!//388K360MSG++#include<stdio.h>#include<string.h>#include<math.h>intC[33][33];//C[n][m],choosemfromn;voidgetCombination(){for(intn=0;n<=32;n++){for(intm=......
  • poj-2282
    //380K 32MS G++#include<stdio.h>#include<string.h>#include<math.h>longlongappearTime1[10];longlongappearTime2[10];voidgetAppearTime(intnum,longlong*appearArray){ appearArray[0]=1; if(num==0){ return; }......
  • poj-2249
    //356K16MSG++//356K0MSG++addm==0check//356K16MSG++//356K0MSG++addm==0check#include<stdio.h>#include<string.h>#include<math.h>intm;intn;//voidgetNum(unsignedintn,unsignedintm){//......
  • poj-1037
    //196K16MSC++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=25;longlongDP[MAX][MAX][2];//0:down.1:upvoidinit(){for(intcurPlankNum=1;curPlankNum<=20;curPlankNum++){for(......
  • poj-2140
    //132K 110MS C++#include<cstring>#include<cstdio>usingnamespacestd;intN;longlongcnt;voidsolve(intN){ intbegin=1; intend=1; longlongsum=1; while(1){ if(begin>N){ break; } //if(begin==......
  • poj-1988
    //564K 282MS C++#include<cstdio>#include<cstring>#include<iostream>usingnamespacestd;structUF_Node{ intup; intcount; intparent;};typedefstructUF_NodeUF_Node;UF_NodeUF_Array[30001];voidcreat(){ intc; for(......
  • poj-1693
    //136K 0MS C++#include<cstdio>#include<cstring>structLine{ intbx,ex; intby,ey;};typedefstructLineLine;LinehLine[110];LinevLine[110];intcaseNum;intLineNum;boolinsect(Line&vline,Line&hline){ //pr......