Tranvick

2006 resp, 1-1

Nov 29th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <cstdio>
  2. #define ll long long
  3. #define forn(i,n) for(int i=1;i<=n;i++)
  4.  
  5. ll pows[20],f,k,a,b,t;
  6.  
  7. void input(){
  8.     freopen("input.txt","r",stdin);
  9.     freopen("output.txt","w",stdout);
  10.     scanf("%I64d%I64d",&f,&k);pows[0]=1;
  11.     forn(i,18) pows[i]=pows[i-1]*10ll;
  12.     ll q=f;
  13.     while (q){
  14.         q/=10ll;
  15.         ++t;
  16.     }
  17. }
  18.  
  19. ll gcd (ll a,ll b,ll &x,ll &y){
  20.     if (a==0ll){
  21.         x=0ll;y=1ll;
  22.         return b;
  23.     }
  24.     ll x1,y1;
  25.     ll g=gcd(b%a,a,x1,y1);
  26.     x=y1-(b/a)*x1;
  27.     y=x1;
  28.     return g;
  29. }
  30.  
  31. ll len(ll x){
  32.     ll ans=0;
  33.     while (x){
  34.         x/=10ll;
  35.         ++ans;
  36.     }
  37.     return ans?ans:1;
  38. }
  39.  
  40. void sol(){
  41.     for(int _=0;_<2;_++,t--){
  42.         a=pows[k]+1ll;b=pows[t]-1ll;
  43.         ll x,y;
  44.         ll g=gcd(a,b,x,y);
  45.         if (f%g!=0) continue;
  46. //      x*=f/g;
  47. //      if (x<0) x+=-x*(b/g);
  48. //      x%=b/g;
  49.         x=((x%(b/g))*((f/g)%(b/g)))%(b/g);
  50.         y=(f-a*x)/b;
  51.         for(;;){
  52.             if (-y==x/pows[t-k] && len(x)==t){
  53.                 printf("%I64d\n",x);
  54.                 return;
  55.             }
  56.             x+=b/g;
  57.             y-=a/g;
  58.             if (x>f) break;
  59.         }
  60.     }
  61. }
  62.  
  63. int main(){
  64.     input();
  65.     sol();
  66.     return 0;
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment