题目链接:http://codeforces.com/contest/1342
A
思路:一个是一个数加一减一,花费a元,一个是两个数同时加一减一,花费b元,那么判断b和2a的大小即可,a肯定是乘以x和y的差值的,加上公共部分即min(x,y)乘以b还是乘以2a,加上他俩最小的即可.
//------------------------------------------------- //Created by HanJinyu //Created Time :日 4/26 22:29:40 2020 //File Name :86A.cpp //------------------------------------------------- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <list> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; typedef double db; typedef long long ll; const int maxn = 200005; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { ll x,y,a,b; scanf("%lld%lld%lld%lld",&x,&y,&a,&b); printf("%lld\n",(max(x,y)-min(x,y))*a+min(min(x,y)*b,min(x,y)*2*a)); } return 0; }View Code
B
思路:给你一个01字符串,要求找到一个字符使得他是所给字符串的子串,且长度<=2len,且该字符串的周期尽可能小,那么直接输出长度为2*n的01字符串即可:10101010...
当所给字符串只有一种字符时,输出原有字符串即可。
//------------------------------------------------- //Created by HanJinyu //Created Time :日 4/26 23:25:55 2020 //File Name :86B.cpp //------------------------------------------------- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <list> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; typedef double db; typedef long long ll; const int maxn = 200005; int main() { // freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d",&T); while(T--) { char str[200]; scanf("%s",str); int len=strlen(str); int _0=0,_1=0; for(int i=0;i<len;i++) { if(str[i]=='0') _0++; else _1++; } if(_0==0||_1==0) printf("%s\n",str); else { for(int i=0;i<len;i++) printf("10"); // printf("1\n"); printf("\n"); } } return 0; }View Code
C
思路:打表样例中的1-200就行了,将i%a%b==i%b%a的数全部列举出来即可,就会发现有规律,即每个a*b里面的满足条件的个数都是一样的,这样就可以用前缀和来写,满足条件的置1,否则为0,再求前缀和,这样所得的就是该区间满足条件的个数了,通过%(a*b)来计算个数就好
//------------------------------------------------- //Created by HanJinyu //Created Time :一 4/27 00:09:16 2020 //File Name :86C.cpp //------------------------------------------------- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <list> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; typedef double db; typedef long long ll; const int maxn = 200005; int main() { // freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; scanf("%d",&T); while(T--) { int a,b,k; scanf("%d%d%d",&a,&b,&k); int sum[maxn]; for(int i=1;i<=a*b;i++) { if(i%a%b!=i%b%a) sum[i]=1; else sum[i]=0; sum[i]+=sum[i-1]; } while(k--) { ll l,r; scanf("%lld%lld",&l,&r); ll ans=(r-l)/(a*b)*sum[a*b]; ll lll=l%(a*b); ll rrr=r%(a*b); if(rrr>=lll) printf("%lld ",ans+sum[rrr]-sum[lll-1]); else printf("%lld ",ans+sum[rrr]+sum[a*b]-sum[lll-1]); } printf("\n"); } return 0; }View Code