luoguP3951 小凯的疑惑/P2662 牛场围栏

其实就是当年sxy给我讲的墨墨的等式,只是当时比较菜听得似懂非懂。

小凯的疑惑

去年noipday1t1,当时随便猜了个结论结果猜对了,现在瞎证一下,答案是a*b-a-b。

设a为a,b中较小的一个,发现b*0%a,b*1%a,b*2%a,b*3%a……b*(a-1)%a的结果两两不同。

反证,如果存在b*x%a=b*y%a(x<y<a),即b*x-b*y=0(mod a),b*(x-y)=0(mod a), ∵gcd(a,b)=1 ∴x-y=0(mod a) 不满足x<y<a,得证

于是mod a等于b*x%a的最小数就是b*x,那么最大的不能表达的数就是b*x-a,于是答案就是b*(a-1)-a=a*b-a-b

 1 //Achen
 2 #include<bits/stdc++.h>
 3 #define For(i,a,b) for(int i=(a);i<=(b);i++) 
 4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
 5 #define Formylove return 0
 6 typedef long long LL;
 7 typedef double db;
 8 using namespace std;
 9 LL a,b;
10 
11 template<typename T>void read(T &x) {
12     char ch=getchar(); T f=1; x=0;
13     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
14     if(ch=='-') f=-1,ch=getchar();
15     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
16 }
17 
18 //#define ANS
19 int main() {
20 #ifdef ANS
21     freopen("1.in","r",stdin);
22     //freopen("1.out","w",stdout);
23 #endif
24     read(a); read(b);
25     printf("%lld\n",a*b-a-b);
26     Formylove;
27 }
View Code

牛场围栏

小凯疑惑的升级版,小L的不疑惑

找出木棍中长度的最小值a,把其他长度拿来跑最短路求出%a=1、2、3……a-1的最小数的大小,答案就是,max(dis[i]-a);

 1 // luogu-judger-enable-o2
 2 //Achen
 3 #include<bits/stdc++.h>
 4 #define For(i,a,b) for(int i=(a);i<=(b);i++) 
 5 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
 6 #define Formylove return 0
 7 const int N=3007;
 8 typedef long long LL;
 9 typedef double db;
10 using namespace std;
11 int a[N],n,m,ok[N],mi,e[N][2],ecnt,vis[N];
12 LL dis[N],inf;
13 
14 template<typename T>void read(T &x) {
15     char ch=getchar(); T f=1; x=0;
16     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
17     if(ch=='-') f=-1,ch=getchar();
18     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
19 }
20 
21 struct node {
22     int v; LL dis;
23     node(int v,LL dis):v(v),dis(dis){}
24     friend bool operator <(const node&A,const node&B) {
25         return A.dis>B.dis;
26     }
27 };
28 priority_queue<node>que;
29 
30 void dijkstra() {
31     memset(dis,127/3,sizeof(dis));
32     dis[0]=0;
33     que.push(node(0,0));
34     while(!que.empty()) {
35         node t=que.top();
36         que.pop();
37         if(vis[t.v]||dis[t.v]!=t.dis) continue;
38         vis[t.v]=1;
39         For(i,1,ecnt) if(dis[(t.v+e[i][0])%mi]>t.dis+e[i][1]) {
40             dis[(t.v+e[i][0])%mi]=t.dis+e[i][1];
41             que.push(node((t.v+e[i][0])%mi,t.dis+e[i][1]));
42         }
43     }
44     LL ans=-1,fl=0;
45     For(i,0,mi-1) {
46         if(dis[i]!=inf) 
47             ans=max(ans,dis[i]-mi);
48         else fl=1;
49     }
50     if(ans==0||fl==1) ans=-1;
51     printf("%lld\n",ans);
52 }
53 
54 //#define ANS
55 int main() {
56 #ifdef ANS
57     freopen("a.in","r",stdin);
58     freopen("a.out","w",stdout);
59 #endif
60     read(n); read(m);
61     memset(dis,127/3,sizeof(dis));
62     inf=dis[0];
63     For(i,1,n) {
64         read(a[i]);
65         ok[a[i]]=1;
66         For(j,1,m) {
67             if(a[i]-j<=0) break;
68             ok[a[i]-j]=1;
69         }
70     }
71     For(i,1,3000) if(ok[i]) {
72         mi=i; break;
73     }
74     Rep(i,3000,1) if(ok[i]) 
75         dis[i%mi]=i;
76     For(i,1,mi-1) if(dis[i]!=inf) {
77         e[++ecnt][0]=i;
78         e[ecnt][1]=dis[i];
79     }
80     dijkstra();
81     Formylove;
82 }
View Code

猜你喜欢

转载自www.cnblogs.com/Achenchen/p/9781596.html