问题 A: 幸运数字III
112
2 1
112=4*4*7
#include
typedef long long ll;
int main()
{
ll n;
while(~scanf("%lld",&n)){
ll ans1=0,ans2=0;
if(n==0){
printf("0 0\n");
continue;
}
for(;;){
if(n%4==0){
n/=4;
ans1++;
}else if(n%7==0){
n/=7;
ans2++;
}else{
printf("%lld %lld\n",ans1,ans2);
break;
}
}
}
return 0;
}
6
3 1 2 4 4 5
1
1 2 3三张编号连续,可以换一张,换完后剩下4 4 5,不符合兑换规则,无法继续兑换。
#include
#include
using namespace std;
int minz(int a,int b,int c)
{
return min(min(a,b),c);
}
int main()
{
int a[100005]={0},n,x,ans=0;
scanf("%d",&n);
for(int i=0;i=1&&a[i+1]>=1&&a[i+2]>=1){
x=minz(a[i],a[i+1],a[i+2]);
ans+=x;
a[i]-=x;a[i+1]-=x;a[i+2]-=x;
}
}printf("%d\n",ans);
return 0;
}
4
b c a b
3
四个位置取出的字符串分别为bcab,cabb,abbc,bbca,显然最小位置是3。
#include
#include
int main()
{
int n;
scanf("%d",&n);
char str[60006],ch[2];
for(int i=0;i
100
41
41=2+3+5+7+11+13
#include
#include
using namespace std;
typedef long long ll;
const int N=1e6+7;
ll x[N],prime[N],cnt=0;
ll sum[N];
void is_prime(int n)
{
for(int i=2;i<=n;i++){
if(x[i]==0){
for(int j=i*2;j<=n;j+=i){
x[j]=2;
}
x[i]=1;
prime[++cnt]=i;
}
}
}
int main()
{
int n,k=0,ans=2,m;
scanf("%d",&n);
is_prime(n);
for(int i=1;i<=cnt;i++){
sum[i]=sum[i-1]+prime[i];
if(x[sum[i]]==1){
k=i;
}
if(sum[i]>n){
m=i;
break;
}
}
int len=k,t=sum[k],tlen=len;
ans=t;
for(int i=1;i<=m;i++){
for(int j=len;i+j<=m;j++){
t=sum[i+j]-sum[i-1];
if(t>n)
break;
if(x[t]==1){
len=max(len,j);
ans=max(ans,t);
}
}
}
printf("%d\n",ans);
return 0;
}
#include
#include
using namespace std;
const int N=1e6+10;
int prime[N],x[N],cnt,n;
void is_prime()
{
for(int i=2;i<=n;i++){
if(x[i]==0){
for(int j=i*2;j<=n;j+=i){
x[j]=2;
}
x[i]=1;
prime[cnt++]=i;
}
}
}
int main()
{
int len=0,ans=0,m;
scanf("%d",&n);
is_prime();
for(int i=0;in) break;
if(x[t]==1&&tl>=len){
ans=t;
len=tl;
}
}
}
printf("%d\n",ans);
return 0;
}
3
1 1
2 1
2 2
1
100%的数据,3<=n<=1000, 1<=x<=100000, 1<=y<=100000
#include
#include
using namespace std;
typedef long long ll;
const int N=1000001;
int main()
{
ll minx=N,miny=N,maxx=-1,maxy=-1;
ll n,x,y;
scanf("%lld",&n);
for(int i=0;i
2 0 1 0 3 0 2
48
台面上共有红球2个、绿球1个、蓝球3个、黑球2个,获得最高分的打法是红-黑-红-黑-绿-蓝-蓝-蓝-黑-黑,共可以获得48分。
保证最后得分不超过2^31-1.
#include
int main()
{
int a[8];
for(int i=1;i<=7;i++){
scanf("%d",&a[i]);
}for(int i=2;i<=7;i++){
int ans=0;
if(a[i]){
int maxz=0;
for(int i=7;i>=2;i--){
if(a[i]){
maxz=i;
break;
}
}int ans=0;
ans+=(maxz+1)*a[1];
for(int i=2;i<=7;i++){
ans+=i*a[i];
}
printf("%d\n",ans);
return 0;
}
}
if(a[1])
printf("1\n");
else{
printf("0\n");
}
return 0;
}
4
1 1 3 2
3
让1+1+1+1 = 4,给定的数字变成4,1,3,2。
#include
#include
using namespace std;
typedef long long ll;
const int N=30000;
const int M=1000000;
int a[N],b[M],c[M];
int main()
{
int n;
scanf("%d",&n);
ll ans=0;
for(int i=0;i1){
for(int j=1;j<=a[n-1]+n;j++){
if(b[j+a[i]]==0){
ans+=j;
b[a[i]]--;
b[j+a[i]]=1;
if(b[a[i]]==1)
break;
}
}
}
}printf("%lld\n",ans);
return 0;
}
4
1 2 3 4
0
#include
#include
using namespace std;
int main()
{
int n;
int sum=0,a[110],dp[10000]={0};
scanf("%d",&n);
for(int i=0;i=a[i];j--){
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
}
s1=max(dp[s],sum-dp[s]);
s2=sum-s1;
int ans=s1*s1-s2*s2;
printf("%d\n",ans);
return 0;
}
3 2 2
4 1 3
2
In the sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket.
#include
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int main()
{
ll n,m,k,a[10020],minodd=inf,mineven=inf;
scanf("%lld%lld%lld",&n,&m,&k);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
if(i%2){
minodd=min(minodd,a[i]);
}else{
mineven=min(mineven,a[i]);
}
}
if(n%2==0){
printf("0\n");
return 0;
}
ll everyadd,minz,temp=n/2+1;
if(n%2){
minz=minodd;
everyadd=m/(temp);
}
ll ans=min(minz,everyadd*k);
printf("%lld\n",ans);
return 0;
}
abc
xyz
-1
#include
using namespace std;
#define MAXN 10000
int nxt[MAXN][26];
int main()
{
string S,T;
cin>>S>>T;
memset(nxt,-1,sizeof(nxt));
int N=S.size();
for(int i=0;i
#include
#include
#include
using namespace std;
char a[10010];
char b[1000010];
vector e[26];
int main(){
scanf("%s%s",a,b);
for(int i=0; a[i]; i++)
e[a[i]-'a'].push_back(i);
int ans=1,now=0;
for(int j=0; b[j]; j++){
int t=b[j]-'a';
if(e[t].empty()){
puts("-1");
return 0;
}
vector::iterator it=lower_bound(e[t].begin(),e[t].end(),now);
if(it==e[t].end()){
ans++;
now=*e[t].begin()+1;
}else
now=*it+1;
}
printf("%d\n",ans);
return 0;
}