Simple Maths 2
如题目所说,这是一道简单数学题。
题目的意思就是求 (lfloorfrac{10^n}{m} floor) 除以 (m) 的余数。
我们看到向下取整的符号可以自然想到一个方法,我们有:
[10^n=lfloorfrac{10^n}{m}
floor imes m+r
]
我们设 (lfloorfrac{10^n}{m} floor=k),则有:
[10^n=k imes m+r
]
[k=k' imes m+r'
]
(r')即为所求。
我们把 (k) 带入到第一个式子中,有:
[10^n=(k' imes m+r') imes m+r
]
[10^n=k' imes m^2+r' imes m+r
]
由余数的定义可得:
[r' imes m+requiv k (operatorname{mod} m^2)
]
[lfloorfrac{r' imes m+r}{m}
floor=r'
]
那么求解 (r') 的方法就明了了。时间复杂度 (O(log m^2))
//Don't act like a loser.
//This code is written by huayucaiji
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read() {
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9') {
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
int n,m;
int qpow(int x,int y) {
int ret=1;
while(y) {
if(y&1) {
ret=ret*x%(m*m);
}
x=x*x%(m*m);
y>>=1;
}
return ret;
}
signed main() {
cin>>n>>m;
cout<<qpow(10,n)/m<<endl;
return 0;
}