hdu 1212

模运算与基本四则运算有些相似,但是除法例外。其规则如下:

      (a + b) % p = (a % p + b % p) % p (1)

      (a - b) % p = (a % p - b % p) % p   (2)

      (a * b) % p = (a % p * b % p) % p  (3)

      (a^b) % p = ((a % p)^b) % p            (4)


结合律:

     ((a+b) % p + c) % p = (a + (b+c) % p) % p (5)

     ((a*b) % p * c)% p = (a * b*c) % p (6)// (a%p*b)%p=(a*b)%p


交换律:

     (a + b) % p = (b+a) % p (7)

     (a * b) % p = (b * a) % p (8)


分配律:

     ((a +b)% p * c) % p = ((a * c) % p + (b * c) % p) % p (9)


重要定理:
    若a≡b (% p),则对于任意的c,都有(a + c) ≡ (b + c) (%p);(10)  
    若a≡b (% p),则对于任意的c,都有(a * c) ≡ (b * c) (%p);(11)

    若a≡b (% p),c≡d (% p),则 (a + c) ≡ (b + d) (%p),(a - c) ≡ (b - d) (%p), (a * c) ≡ (b * d) (%p),(a / c) ≡ (b / d) (%p); (12)

  用秦九韶公式和模运算规律展开式子,如:123%9=((1*10+2)*10+3)%9=((1*10+2)%9*10+3)%9=((1%9*10+2)%9*10+3)%9,接下来用程序递归实现就行了。

代码如下:

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-8;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const int M=20010;
const int N=500010;
const int d[][2] = { {0,1},{0,-1},{-1,0},{1,0} };
typedef pair<int, int> pii;
int n;
int main()
{
    char a[1010];
    int b;
    while(~scanf("%s%d",&a,&b))
    {
        LL ans=0;
        for (int i=0;a[i];i++){
            ans=(ans*10+a[i]-'0')%b;
        }
        printf("%lld\n",ans);
    }
}
































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值