直接用除法算式模拟即可。
需要注意的是要先用while扩大被除数(要找的光棍数),后续 只用if 即可
#include<bits/stdc++.h> using namespace std; long long x; long long cnt=1; long long k=1; int main() { cin>>x; while(k<x) k=(k<<3)+(k<<1)+1,++cnt; printf("%lld",k/x); k%=x; while(k) { if(k<x) k=(k<<3)+(k<<1)+1,++cnt; printf("%lld",k/x); k%=x; } cout<<' '<<cnt; return 0; }