HDU 1212 Big Number

题意:给一数字字符串s ( n=s.size()<=1000 )   和数字m (<1e5) 求s%m

模拟除法, k初值0,按s[0]...累乘相加,把字符串还原成数字,比m大时-m,继续按位还原到s[n-1]

此时剩下的k再%m即为所求

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<list>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> p;
typedef long double ld;
#define mem(x) memset(x, 0, sizeof(x))
#define me(x) memset(x, -1, sizeof(x))
#define fo(i,n) for(i=0; i<n; i++)
#define sc(x) scanf("%lf", &x)
#define pr(x) printf("%lld
", x)
#define pri(x) printf("%lld ", x)
#define lowbit(x) x&-x
const ll MOD = 1e18 +7;
const ll N = 6e6 +5;
ll a[N], vis[N];
int main()
{
    ll i, j, k, l=0;
    ll n, m, t, x;
    string s, s1;

    while(cin>>s>>m)
    {
        k=0;i=0;n=s.size();
        while(1)
        {
            if(i>=n) break;
            while(k<m && i<n)
            {
                k*=10;
                k+=s[i++]-'0';
                if(k>=m) break;
            }
            k-=m;
        }
        if(k<0) k+=m;
        cout<<k%m<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/op-z/p/10751296.html