Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #define ll long long
- #define forn(i,n) for(int i=1;i<=n;i++)
- ll pows[20],f,k,a,b,t;
- void input(){
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- scanf("%I64d%I64d",&f,&k);pows[0]=1;
- forn(i,18) pows[i]=pows[i-1]*10ll;
- ll q=f;
- while (q){
- q/=10ll;
- ++t;
- }
- }
- ll gcd (ll a,ll b,ll &x,ll &y){
- if (a==0ll){
- x=0ll;y=1ll;
- return b;
- }
- ll x1,y1;
- ll g=gcd(b%a,a,x1,y1);
- x=y1-(b/a)*x1;
- y=x1;
- return g;
- }
- ll len(ll x){
- ll ans=0;
- while (x){
- x/=10ll;
- ++ans;
- }
- return ans?ans:1;
- }
- void sol(){
- for(int _=0;_<2;_++,t--){
- a=pows[k]+1ll;b=pows[t]-1ll;
- ll x,y;
- ll g=gcd(a,b,x,y);
- if (f%g!=0) continue;
- // x*=f/g;
- // if (x<0) x+=-x*(b/g);
- // x%=b/g;
- x=((x%(b/g))*((f/g)%(b/g)))%(b/g);
- y=(f-a*x)/b;
- for(;;){
- if (-y==x/pows[t-k] && len(x)==t){
- printf("%I64d\n",x);
- return;
- }
- x+=b/g;
- y-=a/g;
- if (x>f) break;
- }
- }
- }
- int main(){
- input();
- sol();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment