题意:
给出一个n;
求出有几组s1,s2 使s1/s2=n;
升序排列;
思路:
暴力枚举除数;
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> #define ll long long using namespace std; ll n; ll s1,s2; priority_queue<ll, vector<ll>, greater<ll> > q; bool judge(ll num) { int vis[10]; memset(vis, 0 ,sizeof(vis)); int l; while(num != 0) { l = num % 10; if(vis[l]) return false; vis[l] = 1; num /= 10; } return true; } void dfs(ll cur, bool f , ll s) { if(f) { ll tmp = cur * n; if(tmp > 9876543210) return; if(judge(tmp)) { q.push(cur); } } for(int i = 0; i < 10; i++) { if(i == 0 && !f) continue; if(!(s & (1 << i))) { dfs(cur * 10 + i,1,s | (1 << i)); } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%lld",&n); dfs(0,0,0); while(!q.empty()) { if(judge(q.top() * n)) { printf("%lld / %lld = %lld\n",q.top() * n,q.top(),n); } q.pop(); } if(t) printf("\n"); } return 0; }
时间: 2024-10-13 02:03:08