A:
之前CF出过一个类似的题目: 3 7 组成的不能够凑成的数最大是 3*7 - (3+7)= 11 ,11 之后的一定可以凑出来;
a,b 互质, a b. 最大不能凑成的数 为 a*b - (a+b)
刚开始没看出来, 脑回路啊!!!
map<int,int>mp;
void init()
{
for(int i=0;i<=50;i++)
{
for(int j=0;j<=50;j++)
{
int x=i*3;
mp[x]=1;
int y=j*7;
mp[y]=1;
int z=x+y;
mp[z]=1;
}
}
}
int main()
{
int n,x;
init();
mp[0]=0;
cin>>n;
while(n--)
{
scanf("%d",&x);
if(mp[x])
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
B;
Vova is again playing some computer game, now an RPG. In the game Vova's character received a quest: to slay the fearsome monster called Modcrab.
After two hours of playing the game Vova has tracked the monster and analyzed its tactics. The Modcrab has h2 health points and an attack power of a2. Knowing that, Vova has decided to buy a lot of strong healing potions and to prepare for battle.
Vova's character has h1 health points and an attack power of a1. Also he has a large supply of healing potions, each of which increases his current amount of health points by c1 when Vova drinks a potion. All potions are identical to each other. It is guaranteed that c1 > a2.
The battle consists of multiple phases. In the beginning of each phase, Vova can either attack the monster (thus reducing its health by a1) or drink a healing potion (it increases Vova's health by c1; Vova's health can exceed h1). Then, if the battle is not over yet, the Modcrab attacks Vova, reducing his health by a2. The battle ends when Vova's (or Modcrab's) health drops to 0 or lower. It is possible that the battle ends in a middle of a phase after Vova's attack.
Of course, Vova wants to win the fight. But also he wants to do it as fast as possible. So he wants to make up a strategy that will allow him to win the fight after the minimum possible number of phases.
Help Vova to make up a strategy! You may assume that Vova never runs out of healing potions, and that he can always win.
The first line contains three integers h1, a1, c1 (1 ≤ h1, a1 ≤ 100, 2 ≤ c1 ≤ 100) — Vova's health, Vova's attack power and the healing power of a potion.
The second line contains two integers h2, a2 (1 ≤ h2 ≤ 100, 1 ≤ a2 < c1) — the Modcrab's health and his attack power.
In the first line print one integer n denoting the minimum number of phases required to win the battle.
Then print n lines. i-th line must be equal to HEAL if Vova drinks a potion in i-th phase, or STRIKE if he attacks the Modcrab.
The strategy must be valid: Vova's character must not be defeated before slaying the Modcrab, and the monster's health must be 0 or lower after Vova's last action.
If there are multiple optimal solutions, print any of them.
10 6 100 17 5
4 STRIKE HEAL STRIKE STRIKE
11 6 100 12 5
2 STRIKE STRIKE
In the first example Vova's character must heal before or after his first attack. Otherwise his health will drop to zero in 2 phases while he needs 3 strikes to win.
In the second example no healing needed, two strikes are enough to get monster to zero health and win with 6 health left.
模拟可以过, 但是要特判, 当最后一次 h2 < a1 h1<a2 时 是可以直接打死不用加血的;
并且决策时 必须是二选一,
int main()
{
int h1,a1,c1;
int h2,a2;
vector<int>Q;
Q.clear();
scanf("%d %d %d",&h1,&a1,&c1);
scanf("%d %d",&h2,&a2);
int ans=0;
while(h2>0)
{
if(h1>a2)
{
ans++;
Q.push_back(0);
h2-=a1;
}
else //if(h1<=a2)
{
if(h2<=a1)
{
ans++;
Q.push_back(0);
h2-=a1;
}
else
{
ans++;
Q.push_back(1);
h1+=c1;
}
}
h1-=a2;
}
cout<<ans<<endl;
for(int i=0;i<Q.size();i++)
{
int t=Q[i];
if(t)
cout<<"HEAL"<<endl;
else
cout<<"STRIKE"<<endl;
}
return 0;
}
C:
C要比B 简单, C题直接找 数目最多的就行了, 小的一定能够放在大的里(前提是一个大的只能放一个小的)
map<ll,ll>mp;
ll a[MAXN];
int main()
{
int n;
scanf("%d",&n);
ll ans=-INF;
for(int i=1;i<=n;i++)
{
scanf("%I64d",&a[i]);
mp[a[i]]++;
ans=max(ans,mp[a[i]]);
}
cout<<ans<<endl;
return 0;
}
D;
Let's denote a function
You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of the array.
Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.
5 1 2 3 1 3
4
4 6 6 5 5
0
4 6 6 4 4
-8
In the first example:
- d(a1, a2) = 0;
- d(a1, a3) = 2;
- d(a1, a4) = 0;
- d(a1, a5) = 2;
- d(a2, a3) = 0;
- d(a2, a4) = 0;
- d(a2, a5) = 0;
- d(a3, a4) = - 2;
- d(a3, a5) = 0;
- d(a4, a5) = 2.
一层for 循环 求 当前数于谦的差值, 减去差值为1 的;
用矩阵面积法求差值; a[i]* (i-1) - sum(前i-1 项之和)
用map 数组标记, 小一的是差值为1 要减去, 大一的 另一半
long long 会爆- - 一个方法是 用long double 一个是用大数封装;
typedef long double ld;
map<ld,ld>mp;
ld a[MAXN];
int main()
{
ll n;
scanf("%I64d",&n);
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
ld sum=0;
ld ans=0;
for(int i=1;i<=n;i++)
{
ll t=a[i]*(i-1)-sum;
sum+=a[i];
ans+= t - mp[a[i]-1]+mp[a[i]+1];
mp[a[i]]++;
}
cout<<fixed<<setprecision(0)<<ans<<endl;
return 0;
}
13