Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long int LL;
- #define inp_s ios_base::sync_with_stdio(false)
- #define DRT() int test_case;cin>>test_case;while(test_case--)
- #define VI vector<int>
- #define VS vector<string>
- #define VLL vector<LL>
- #define PII pair<LL,LL>
- #define all(c) c.begin(),c.end()
- #define sz(c) c.size()
- #define clr(c) c.clear()
- #define msi map<string,int>
- #define msit map<string,int>::iterator
- #define pb push_back
- #define mp make_pair
- #define GI(x) scanf("%d",&x)
- #define FOR(i,a,b) for(int i=a;i<b;i++)
- #define RFOR(i,a,b) for(int i=b-1;i>=a;i--)
- #define gcd(a,b) __gcd(a,b)
- #define MOD 1000000007
- #define EPS 1E-10
- #define ELR 2.71828182845904523536
- #define PI acos(-1)
- #define CASE(x) cout<<"Case "<<x<<": ";
- typedef struct
- {
- string s;
- int mod;
- int sod;
- } node;
- node getNode(string ans,int mod,int sod)
- {
- node ret;
- ret.s = ans;
- ret.mod = mod;
- ret.sod = sod;
- return ret;
- }
- int main()
- {
- inp_s;
- DRT()
- {
- int n;
- cin >> n;
- queue<node> bfs;
- bfs.push(getNode("",0,0));
- int visited[n][n+1];
- FOR(i,0,n) FOR(j,0,n+1) visited[i][j] = 0;
- visited[0][0] = 1;
- while(!bfs.empty())
- {
- node top = bfs.front();
- bfs.pop();
- if(top.mod == 0 && top.sod == n)
- {
- cout << top.s << endl;
- break;
- }
- int lwr = 1;
- if(top.sod) lwr = 0;
- FOR(i,lwr,10)
- {
- int nmod = (top.mod * 10 + i) % n;
- int nsod = (top.sod + i);
- if(nsod <= n && !visited[nmod][nsod])
- {
- visited[nmod][nsod] = 1;
- string q = top.s + (char)('0' + i);
- bfs.push(getNode(q,nmod,nsod));
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement