国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

1046 A^B Mod C

2019-11-11 02:52:02
字體:
供稿:網(wǎng)友

思路:先得到三個數(shù), 然后對A求冪 再模c,然后得余數(shù)  我最開始呢老是先得到余數(shù)再去求模,但是老是超時,然后看別人都是折倍求  效率提高很多,總結(jié)經(jīng)驗有下面兩個程序,本質(zhì)是一樣的

------------------------------------------------------------------------------------------------------------------------------------------------

using System;using System.IO;using System.Linq;using System.Numerics;public class PRogram{    public static void Main(string[] args)    {        var sr = new StreamReader(Console.OpenStandardInput());        var sw = new StreamWriter(Console.OpenStandardOutput());        string[] s = sr.ReadLine().Split(' ');        var a = BigInteger.Parse(s[0]);        var b = BigInteger.Parse(s[1]);        var c = BigInteger.Parse(s[2]);       var  ret = 1;        while (b > 0)        {            if ((b % 2) > 0)            {                ret = (int)((ret * a) % c);            }            a = (a * a) % c;            b /= 2; ;        }        sw.WriteLine(ret);       sr.Close();        sw.Close();    }}

-----------------------------------------------------------------------------------------------------------------------------------

這個程序的變量要是定義成int類型,測試數(shù)據(jù)時會有錯誤,所以換成了long

using System;using System.IO;namespace 求模{    class program    {        static void Main(string [] str)        {            StreamReader sr = new StreamReader(Console.OpenStandardInput());            string[] s = sr.ReadLine().Split(' ');           long a = Convert.ToInt32(s[0]);            long  b = Convert.ToInt32(s[1]);            long c = Convert.ToInt32(s[2]);            long  result = 1;            while(b>0)            {                if(b%2!=0)                {                    result = (result * a) % c;                }                a = (a * a)%c;                b = b / 2;            }            Console.WriteLine(result);        }    }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜兰县| 吴旗县| 胶南市| 双鸭山市| 武陟县| 襄城县| 平原县| 教育| 同德县| 京山县| 临江市| 句容市| 会泽县| 义乌市| 江孜县| 伊吾县| 西城区| 乌拉特前旗| 武川县| 高清| 三穗县| 靖西县| 富平县| 年辖:市辖区| 安远县| 石林| 达日县| 高州市| 巢湖市| 丰顺县| 新营市| 鹤壁市| 潞城市| 长泰县| 武夷山市| 班戈县| 荔浦县| 杨浦区| 开江县| 米脂县| 三亚市|