zoukankan      html  css  js  c++  java
  • 2875: [Noi2012]随机数生成器

    Description
    Input

    包含6个用空格分割的m,a,c,X0,n和g,其中a,c,X0是非负整数,m,n,g是正整数。

    Output

    输出一个数,即Xn mod g
    Sample Input

    11 8 7 1 5 3


    Sample Output
    2

    快速幂+快速乘

     1 type
     2     matrix=array[1..2,1..2]of int64;
     3 var
     4     a,c,p,x0,n,g:int64;
     5     x,y:matrix;
     6  
     7 function kc(x,y:int64):int64;
     8 begin
     9     if y=0 then exit(0);
    10     kc:=kc(x,y>>1);
    11     kc:=(kc+kc)mod p;
    12     if y and 1=1 then kc:=(kc+x)mod p;
    13 end;
    14  
    15 operator *(a,b:matrix)c:matrix;
    16 var
    17     i,j,k:longint;
    18 begin
    19     fillchar(c,sizeof(c),0);
    20     for i:=1 to 2 do
    21         for j:=1 to 2 do
    22             for k:=1 to 2 do
    23                 c[i,k]:=(c[i,k]+kc(a[i,j],b[j,k]))mod p;
    24 end;
    25  
    26 procedure main;
    27 begin
    28     read(p,a,c,x0,n,g);
    29     x[1,1]:=1;x[2,2]:=1;
    30     y[1,1]:=a;y[2,1]:=c;y[2,2]:=1;
    31     while n>0 do
    32         begin
    33             if n and 1=1 then x:=x*y;
    34             y:=y*y;
    35             n:=n>>1;
    36         end;
    37     writeln((kc(x0,x[1,1])+x[2,1])mod p mod g);
    38 end;
    39  
    40 begin
    41     main;
    42 end.
    View Code
  • 相关阅读:
    PHP Mail 简介
    二级叉的结算另一种方法
    PHP extract() 函数
    array_count_values函数
    对碰结算的方法
    array_walk() 函数
    函数引发二级叉的结算
    array_intersect() php筛选两个数组共有的元素
    php文件锁
    JAVA Math常用方法
  • 原文地址:https://www.cnblogs.com/Randolph87/p/3789663.html
Copyright © 2011-2022 走看看