题解:AT_abc448_e [ABC448E] Simple Division

· · 题解

Vjudge 中文题面传送门

解题思路

题目要求 \frac{N}{M} \bmod 10007 ,这里不能先模上10007再去除以M,那么我们可以转变一下求值的方法:N%(M*10007)/M%10007 。

那么剩下最关键的就是怎么求N值了。

初始令 N=0 ,然后每次加 l_i 个 c_i ,相当于做 l_i 次 N=N * 10+c_i

但是题目中 1<= l_i <=10^9 ,直接暴力模拟肯定会 TLE ,所以我们可以借鉴一下矩阵快速幂的思想,具体看代码。

下次我们换一个不同的C继续做这个操作

假设第一次 f(r) = a1*r + b1 ,

第二次 g(r) = a2*r + b2 。

那么两次叠加 g(f(r)) = (a2*a1)r + (a2*b1 + b2) 。

我们可以得到 第一个参数 a = a2*a1 ,第二个参数 b = a2*b1 + b2 。

这里使用类似矩阵快速幂的方法快速计算 l_i 次乘法。

::::success[AC code]

#include<bits/stdc++.h>
#define ull unsigned long long
#define int long long//如果涉及到取模操作最好开个long long
using namespace std;
int n,m;
int mod;
int ans;
struct node{
    int x;
    int y;
    node friend operator*(const node p,const node q){
        return {p.x*q.x%mod,(p.y*q.x%mod+q.y)%mod};
    }//重载运算符 *
};
node pows(node x,int y){
    node tot={1,0};
    while(y){
        if(y&1) tot=tot*x;
        x=x*x;
        y>>=1;
    }
    return tot;
}//“快速幂”
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    cin>>n>>m;
    mod=m*10007;
    for(int i=1;i<=n;i++){
        int x,y;
        node k;
        cin>>x>>y;
        k=pows({10,x},y);
        ans=(ans*k.x%mod+k.y)%mod;
    }
    ans=ans/m%mod;//最后这步别忘了
    cout<<ans;
    return 0;
}

::::

本蒟蒻没有还过审的题解,管理大大求过