A.GCD Path http://ch.ezoj.tk/contest/CH%20Round%20%2353%20-%E3%80%90Nescaf%C3%A9%2032%E3%80%91%E6%9D%AFNOIP%E6%A8%A1%E6%8B%9F%E8%B5%9B/GCD%20Path

题解:我想掀桌。。。

          刚开始没思路,打了个30分的暴力把结果打出来,找规律。

          发现x-y的最短路貌似就是 x/gcd(x,y)的质因数分解加起来?

          果断 for 2 to sqrt()  枚举。。。。。。。。。。。。。。。。。。

          还以为能AC,妈蛋,爆零了。。。。。。。。。。。。。。。。

          你以为质因数分解是判素数????????????????

          呵呵。。。。。。。 呵呵。。。。。。。。呵呵。。。。。。。

          加上 if(x==1)ans+=x 瞬间50。。。。。。。。。。。。。。。。

          加上 if(x==y)cout<<0<<endl 瞬间AC。。。。。。。。。。。。。

          极度郁闷中。。。。。。。。。。。。。。。。。。。。。。。

          如果我会用linux的diff的话大概会和暴力比较一下,结果只看了100的数据就以为对了。。。。

          如果我10.30走的话也许会在对拍一下。。。。。。。。。。。

          没办法,还是人太弱。。。。。。。。。。。。。。。。。。。

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 500+100
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 
45 using namespace std;
46 
47 inline int read()
48 
49 {
50 
51     int x=0,f=1;char ch=getchar();
52 
53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
54 
55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
56 
57     return x*f;
58 
59 }
60 int n,m;
61 inline int gcd(int x,int y){return y?gcd(y,x%y):x;}
62 
63 int main()
64 
65 {
66 
67     freopen("input.txt","r",stdin);
68 
69     freopen("output.txt","w",stdout);
70 
71     n=read();m=read();
72     while(m--)
73     {
74         int x=read(),y=read(),t=(int)sqrt(x),ans=0;
75         if(x==y){cout<<0<<endl;continue;}
76         x/=gcd(x,y);if(x==1){cout<<1<<endl;continue;};
77         for2(i,2,t)if(x%i==0)
78         {
79             while(x%i==0)x/=i,ans+=i;
80         }
81         if(x!=1)ans+=x;
82         printf("%d\n",ans);
83     }
84 
85     return 0;
86 
87 }
View Code

相关文章:

  • 2021-11-17
  • 2021-06-11
  • 2021-09-07
  • 2022-01-07
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
猜你喜欢
  • 2022-01-17
  • 2021-06-18
  • 2021-05-28
  • 2021-09-10
  • 2021-07-19
  • 2021-07-23
  • 2021-11-16
相关资源
相似解决方案