码迷,mamicode.com
首页 > 其他好文 > 详细

同余模定理的应用

时间:2017-10-02 21:33:14      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:input   std   names   log   nbsp   输出   lld   str   rip   

一、公式

同余模定理:
(a+b)%mod=( a%mod + b%mod )%mod
(a*b)%mod=( (a%mod) * (b%mod) )%mod

 

二、应用

求S

Description

S(n)=n^5

求S(n)除以3的余数

Input

每行输入一个整数n,(0 < n < 1000000)  //注意n的范围,int、long long int 都会爆掉

处理到文件结束

Output

输出S(n)%3的结果并换行
Sample Input

1

2

Sample Output

1

2

三、代码:

#include<stdio.h>
#include<iostream>
using namespace std;

int main()
{
    long long int n;
    while(~scanf("%lld",&n))
    {
        long long int s=n;
        ///同余模定理:   
        for(int i=1;i<5;i++)
        {
            s=((s%3)*(n%3));
        }
        ///printf("%d\n",s);
        printf("%lld\n",s%3);
    }
    return 0;
}

 

同余模定理的应用

标签:input   std   names   log   nbsp   输出   lld   str   rip   

原文地址:http://www.cnblogs.com/hhkobeww/p/7622410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!