Jzzhu and Children
There are n children in Jzzhu’s school. Jzzhu is going to give some candies to them. Let’s number all the children from 1 to n. The i-th child wants to get at least ai candies.
Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:
Give m candies to the first child of the line.
If this child still haven’t got enough candies, then the child goes to the end of the line, else the child go home.
Repeat the first two steps while the line is not empty.
Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?
水
#include<bits/stdc++.h>
using namespace std;
int main()
{int m,n,i,maxn,ret,a[110];
cin>>n>>m;
for(i=1;i<=n;i++)
{int flag=0;
scanf("%d",&a[i]);
if(a[i]%m==0)flag=1;
a[i]/=m;
a[i]-=flag;
}
maxn=a[1];ret=1;
for(i=2;i<=n;i++)
{if(a[i]>=maxn)
{maxn=a[i];ret=i;}
}
cout<<ret;
}
Jzzhu and Sequences
Jzzhu has invented a kind of sequences, they meet the following property:
You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
Input
The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).
Output
Output a single integer representing fn modulo 1000000007 (109 + 7).
看上去是要枚举,其实是6个一循环。。害得我刚开始还快写完半个矩阵乘法。。
ps:记得要加2个mod取摸防止负数。
#include<bits/stdc++.h>
using namespace std;
long long int mod=1e9+7;
int main()
{long long int x,y,n;
cin>>x>>y>>n;
n=n%6;
switch(n)
{case 0:cout<<(x-y+3*mod)%mod;break;
case 1:cout<<(x+3*mod)%mod;break;
case 2:cout<<(y+3*mod)%mod;break;
case 3:cout<<(y-x+3*mod)%mod;break;
case 4:cout<<(-x+3*mod)%mod;break;
case 5:cout<<(-y+3*mod)%mod;break;
}
}
Jzzhu and Chocolate
Jzzhu has a big rectangular chocolate bar that consists of n × m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements:
each cut should be straight (horizontal or vertical);
each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut);
each cut should go inside the whole chocolate bar, and all cuts must be distinct.
The picture below shows a possible way to cut a 5 × 6 chocolate for 5 times.
Imagine Jzzhu have made k cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactly k cuts? The area of a chocolate piece is the number of unit squares in it.
分巧克力。讨论四种情况即可(只切行,只切列,切完行再切列,切完列再切行)
ps:记得long long 数据是相乘的会爆int
#include<bits/stdc++.h>
using namespace std;
//long long int mod=1e9+7;
long long int n,m,nn,mm,maxn=-9999;
void Max() {//交换,m是行和列中大的那货
if(n>m) {
n=n+m;
m=n-m;
n=n-m;
}
}
int main() {
long long int k,i,sum;
cin>>n>>m>>k;
sum=n*m;
if(k>n+m-2) {
cout<<-1;
return 0;
}
Max();
nn=n;
mm=m;
if(k<=n-1) {
nn/=k+1;
maxn=max(nn*m,maxn);
}
if(k<=m-1) {
mm/=k+1;
maxn=max(mm*n,maxn);
}
if(k>n-1) {
maxn=max(maxn,m/(k-n+2));
}
if(k>m-1) {
maxn=max(maxn,n/(k-m+2));
}
cout<<maxn;
}
Jzzhu and Apples
Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store.
Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.
Jzzhu wonders how to get the maximum possible number of groups. Can you help him?
分苹果成一对一对,保证每对最大公约数大于1.思路就是先埃氏筛质数,之后从一半向前枚举质数,如果这个质数的倍数个数是偶数那么全make-pair到答案里,如果不是则保留第二个(一定能被2整除,因为2是最6的)。
#include<bits/stdc++.h>
using namespace std;
int main() {
bool b[100010]= {0},c[100010]= {0};
int n,i,j,p=1,a[100010];
vector <pair<int,int> >ans;
//freopen("in.txt","w",stdout);
cin>>n;
for(i=2; i<=n/2+1; i++) {
if(b[i]==1)continue;
a[p]=i;
p++;
for(j=2*i; j<=n/2+1; j=j+i)
b[j]=1;
}
for(i=n/2; i>=2; i--) {
if(b[i]==0) {
bool flg=1,f=0;
int x,jj=0,xx=0;
x=i;
for(j=i*2; j<=n; j=j+i) {
if(c[j]==1)continue;
else if(flg==0)flg=1,x=j;
else {
if(f!=0)
ans.push_back(make_pair(j,x)),c[j]=1,c[x]=1;
else if(f==0)jj=j,xx=x;
flg=0;
f++;
}
}
j-=i;
if(c[j]==0&&j%2==1&&xx!=0&&jj!=0&&x!=0) {
c[j]=1;
c[xx]=1;
ans.push_back(make_pair(j,xx));
} else if(jj!=0&&xx!=0) {
c[jj]=1;
c[xx]=1;
ans.push_back(make_pair(jj,xx));
}
}
}
cout<<ans.size()<<endl;
vector<pair<int,int> > ::iterator iter;
iter=ans.begin();
for(iter=ans.begin(); iter!=ans.end(); iter++) {
cout<<iter->first<<" "<<iter->second<<endl;
}
}