A. Div. 7
思路:
$注意n>=10,说明一定是两位数$
$并且7个一循环,直接枚举这位数的最后一位$
$暴力判断是否可行即可$
时间复杂度:$Ot$
#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int>
#define re register int
#define lb lower_bound
#define ub upper_bound
#define int long long
#define pb push_back
#define y second
#define x first
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;}
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
signed main()
{
cf
{
int n ;
cin >> n ;
if(n % 7 == 0) de(n) ;
else
{
string s = to_string(n) ;
for(char i = '0' ; i <= '9' ; i ++)
{
s[sz(s) - 1] = i ;
if(stoi(s) % 7 == 0)
{
de(s) ;
break ;
}
}
}
}
return 0;
}
B. Minority
思路:
$假设该字符串0的数量为a$
$1的数量为b$
$如果a=b,显然答案为max(a-1,0)$
$如果a>b,答案为b$
$如果a<b,答案为a$
时间复杂度:$On$
#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int>
#define re register int
#define lb lower_bound
#define ub upper_bound
#define int long long
#define pb push_back
#define y second
#define x first
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;}
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
signed main()
{
cf
{
string s ;
int s1 = 0 , s2 = 0 ;
cin >> s ;
for(auto i : s)
{
if(i == '1') s1 ++ ;
else s2 ++ ;
}
if(s1 == s2) de(max(s1 - 1 , 0ll)) ;
else if(s1 > s2) de(s2) ;
else de(s1) ;
}
return 0;
}
C. Kill the Monster
思路:
$注意到k的总和为2e5$
$那么我们可以枚举生命hc加i次,攻击dc加k-i次$
$o1判断即可$
$时间复杂度ok$
$如何o1判断$
$敌人杀死我需要\lceil \frac{hc}{dm} \rceil个回合$
$我杀死敌人需要\lceil \frac{hm}{dc} \rceil个回合$
$即$
$$\lceil \frac{hc}{dm} \rceil >= \lceil \frac{hm}{dc} \rceil$$
时间复杂度:$Ok$
#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int>
#define re register int
#define lb lower_bound
#define ub upper_bound
#define int long long
#define pb push_back
#define y second
#define x first
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;}
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
bool check(int hc , int dc , int hm , int dm)
{
int len = (hc + dm - 1) / dm ;
if(len * dc >= hm) return 1 ;
else return 0 ;
}
signed main()
{
cf
{
int hc , dc , hm , dm , k , w , a ;
sf2(hc , dc) ;
sf2(hm , dm) ;
sf(k) ;
sf2(w , a) ;
int f1 = 0 ;
for(int i = 0 ; i <= k ; i ++)
{
int j = k - i ;
if(check(hc + i * a , dc + j * w , hm , dm )) f1 = 1 ;
}
if(f1) puts("YES") ;
else puts("NO") ;
}
return 0;
}
D. Make Them Equal
思路:
$注意到每次a[i]变成b[i]都是从1开始变化的$
$也就是说我们可以先预处理一个数组d[i]数组$
$表示1变化到i的最小操作数$
$注意到b[i]的最大值是1e3$
$我们可以暴力bfs用O1e6的时间复杂度预处理该数组$
$然后在把题目转换一下$
$操作数最多是k$
$对于每个i,变成b[i]的操作数为d[i]$
$你可以得到c[i]个硬币$
$显然,操作数k我们可以看作是背包体积$
$对于每个i,变成b[i]的操作数为d[i],可以看作是物品i的体积为d[i]$
$你可以得到c[i]个硬币,可以看作是物品i的价值c[i]$
$每个物品最多选一次,跑01背包即可$
$注意到d[i]数组的最大值为12$
$也就是说背包体积最大为12*1e3$
$假设背包体积为m,m=min(12*n,k)$
$时间复杂度=min(12*n,k)*n + n^2 \approx 1e7$
时间复杂度:$Omin(12*n,k)*n + n^2$
#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int>
#define re register int
#define lb lower_bound
#define ub upper_bound
#define int long long
#define pb push_back
#define y second
#define x first
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;}
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
int n , k ;
int b[N] ;
int c[N] ;
int d[N] ;
int f[N] ;
signed main()
{
// bfs预处理d数组
memset(d , -1 , sizeof d) ;
d[1] = 0 ;
queue<int> q ;
q.push(1) ;
while(q.size())
{
auto t = q.front() ;
q.pop() ;
for(int j = 1 ; j <= t ; j ++)
{
int x = t + t / j ;
if(x > 1e3) continue ;
if(d[x] != -1 || x == 1) continue ;
d[x] = d[t] + 1 ;
q.push(x) ;
}
}
cf
{
cin >> n >> k ;
fer(i,1,n) sf(b[i]) ;
fer(i,1,n) sf(c[i]) ;
int m = min(k,20000*1ll) ;
fer(i,0,m) f[i] = 0 ;
// 01背包
for(int i = 1 ; i <= n ; i ++)
{
for(int j = m ; j >= d[b[i]] ; j --)
f[j] = max(f[j],f[j - d[b[i]]] + c[i]);
}
de(f[m]) ;
}
return 0;
}
A 题面 的minimum number 好像没有这个限制条件啊
是没有 我写的有问题吗
You have to change the minimum number of digits in it in such a way that the resulting number does not have any leading zeroes and is divisible by 7. 只是说更改的数位要最少 只改最后一个有问题吗
明白了,我以为是最小的数字呢