LightOj 1215 - Finding LCM
// Verdict:: Accepted #include <iostream> #include <cstdio> #include <cstring> #include <map> #include <string> #include <vector> #include <cmath> #include <cctype> #include <sstream> #include <set> #include <list> #include <stack> #include <queue> #include <algorithm> #define sf scanf #define pf printf #define sfint(a,b) scanf("%d %d",&a,&b) #define sfl(a,b) scanf("%ld %ld",&a,&b) #define sfll(a,b) scanf("%lld %lld",&a,&b) #define sfd(a,b) scanf("%lf %lf",&a,&b) #define sff(a,b) scanf("%f %f",&a,&b) #define lp1(i,n) for(i=0;i<n;i++) #define lp2(i,n) for(i=1;i<=n;i++) #define LL long long #define L long #define mem(c,v) memset(c,v,sizeof(c)) #define ui unsigned int #define cp(a) cout<<" "<<a<<" "<<endl #define ull unsigned long long int #define nl puts("") #define sq(x) ((x)*(x)) #define all(x) x.begin(),x.end() #define mx7 20000100 #define mx6 1500000 #define mx5 100005 #define inf 1<<30 //infinity value #define eps 1e-9 #define mx (65540) #define mod 1000000007 #define pb push_back #define pi acos(-1.0) #define sz size() #define gc getchar () using namespace std; //.................................................................................................................. template<class T> T setbit(T n, T pos){n=n|(1<<pos); return n;} template<class T> T checkbit(T n, T pos){n=n&(1<<pos); return n;} template<class T> T gcd(T a, T b ) {return b<=0?a:gcd(b,a%b);} template<class T> T lcm(T a, T b) {return ((a / gcd(a,b) ) * b);} template<class T> T large(T a, T b ) {return a>b?a:b;} template<class T> T small(T a, T b ) {return a<b?a:b;} int main() { int tc,t=0; sf("%d",&tc); while(tc--) { ull a,b,x; sf("%llu %llu %llu",&a,&b,&x); ull r = lcm<ull>(a,b); //cout << r; if(!(x % r)) { ull c = x / r; ull ans,d=gcd(c,x); if(d==1) { pf("Case %d: %llu\n",++t,c); } else { ans=1; while(d != 1) { d=gcd(c,x); x/=d; ans*=d; } pf("Case %d: %llu\n",++t,ans); } } else { pf("Case %d: impossible\n",++t); } } return 0; }
================ **** Recursion Manner ****============================
// Verdict:: Accepted #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <map> #include <string> #include <algorithm> #define sf scanf #define pf printf #define LL long long #define Lo long #define ull unsigned long long #define ul unsigned long #define ui unsigned int #define mx 1000100 using namespace std; template<class T> T gcd(T a, T b){return b<=0?a:gcd(b,a%b);} template<class T> T lcm(T a, T b){return (a / (gcd(a,b))) * b; } template<class T> T setbit(T n, T pos) {return (n|(1<<pos));} template<class T> T checkbit(T n, T pos) {return (n&(1<<pos));} LL find_one(LL c, LL L) { LL d=gcd<LL>(c,L),ans=1; //LL d; //cout << "c = " << c << endl; //cout << "d = " << d << endl; L=L/d; //cout << "L = " << L << endl; if(gcd<LL>(c,L)==1) return d; //else //{ //ans*=d; d = d * find_one(c , L); //} //cout << "d = " << d; return d; } int main() { int tc,t=0; cin >> tc; while(tc--) { LL a,b,L; sf("%lld %lld %lld",&a,&b,&L); LL res=lcm<LL>(a,b); //cout << "res = " << res; LL modo=L%res; LL take; if(!modo) { LL divi=L/res; LL c=gcd<LL>(divi,L); //cout << "c = " << c; if(c==1) { pf("Case %d: %lld\n",++t,L/res); } else { //cout << "c = " << c << endl; //cout << "L = " << L << endl; LL take=find_one(c,L); //find_one(c,L); pf("Case %d: %lld\n",++t,take); } //pf("Case %d: %lld\n",++t,take); } else { pf("Case %d: impossible\n",++t); } } return 0; }
ae problem r solve technique ba analysis likhle valo hoto
ReplyDelete