Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比
赛计算GCD。有一天Sheng bill很嚣张地找到了你,并要求和你比 赛,但是输给Sheng bill岂不是很丢脸!所以你
决定写一个程序来教训他。
标签:set ons ast priority amp tchar read content rip
一行,表示A和B的最大公约数。
Day1
真想把这题R了,(怒)
找了一个下午的错,,
想法其实很简单,就是裸的高精度gcd。
于是用更相减损术就可以了,但是仔细一算发现会TLE这时就要压位了,
有转念一想为什么不用辗转整除做呢,
正兴致勃勃地打代码时突然想到这是压位的高精度模呀,R,
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld; 11 typedef pair<int,int> pr; 12 const double pi=acos(-1); 13 #define rep(i,a,n) for(int i=a;i<=n;i++) 14 #define per(i,n,a) for(int i=n;i>=a;i--) 15 #define Rep(i,u) for(int i=head[u];i;i=Next[i]) 16 #define clr(a) memset(a,0,sizeof(a)) 17 #define pb push_back 18 #define mp make_pair 19 #define fi first 20 #define sc second 21 #define pq priority_queue 22 #define pqb priority_queue <int, vector<int>, less<int> > 23 #define pqs priority_queue <int, vector<int>, greater<int> > 24 #define vec vector 25 ld eps=1e-9; 26 ll pp=1000000007; 27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;} 28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;} 29 //void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } 30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; } 31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1}; 32 ll read(){ ll ans=0; char last=‘ ‘,ch=getchar(); 33 while(ch<‘0‘ || ch>‘9‘)last=ch,ch=getchar(); 34 while(ch>=‘0‘ && ch<=‘9‘)ans=ans*10+ch-‘0‘,ch=getchar(); 35 if(last==‘-‘)ans=-ans; return ans; 36 } 37 #define P 1000000000 38 #define N 10005 39 char ch[N]; 40 struct Hp{ 41 int len,nu[N/9+5]; 42 Hp(){ 43 len=0; memset(nu,0,sizeof(nu)); 44 } 45 void read(){ 46 scanf("%s",ch); int slen=strlen(ch); 47 if (slen%9==0) len=slen/9; 48 else len=slen/9+1; 49 for (int i=1;i<=len;i++){ 50 int g=1; 51 for (int j=0;j<9;j++) 52 if (slen-1-(i-1)*9-j>=0) { 53 nu[i]+=(ch[slen-1-(i-1)*9-j]-‘0‘)*g; 54 g*=10; 55 } 56 } 57 } 58 void div2(){ 59 for (int i=len;i>0;i--) { 60 if (nu[i]&1) nu[i-1]+=P; 61 nu[i]>>=1; 62 } 63 if (nu[len]==0) len--; 64 } 65 void mul2(){ 66 for (int i=1;i<=len;i++) nu[i]<<=1; 67 for (int i=1;i<=len;i++) 68 if (nu[i]>=P) nu[i]-=P,nu[i+1]++; 69 if (nu[len+1]>0) len++; 70 } 71 friend bool operator >(Hp a,Hp b){ 72 if (b.len>a.len) return 0; 73 if (b.len<a.len) return 1; 74 for (int i=a.len;i>0;i--){ 75 if (a.nu[i]>b.nu[i]) return 1; 76 if (a.nu[i]<b.nu[i]) return 0; 77 } 78 return 1; 79 } 80 friend Hp operator -(Hp a,Hp b){ 81 Hp c; c.len=a.len; 82 for (int i=a.len;i>0;i--) 83 c.nu[i]=a.nu[i]-b.nu[i]; 84 for (int i=1;i<=c.len;i++) 85 if (c.nu[i]<0) c.nu[i+1]--,c.nu[i]+=P; 86 while (c.nu[c.len]==0&&c.len) c.len--; 87 return c; 88 } 89 bool z(){ 90 for (int i=len;i>0;i--) 91 if (nu[i]>0) return 0; 92 return 1; 93 } 94 void out(){ 95 for (int i=len;i>0;i--) 96 if (i==len) printf("%d",nu[i]); 97 else printf("%09d",nu[i]); 98 } 99 }; 100 int main() 101 { 102 Hp a,b; a.read(); b.read(); int num=0; 103 while(1) 104 { 105 if((a.nu[1]%2==0)&&(b.nu[1]%2==0)){a.div2();b.div2();num++;} 106 else if((a.nu[1]%2==0)) a.div2(); 107 else if((b.nu[1]%2==0)) b.div2(); 108 if(a>b){a=a-b; if(a.z()){while(num--)b.mul2();b.out();break;}} 109 else {b=b-a; if(b.z()){while(num--)a.mul2();a.out();break;}} 110 } 111 return 0; 112 }
标签:set ons ast priority amp tchar read content rip
原文地址:http://www.cnblogs.com/SXia/p/6817316.html