The following problem is well-known: given integers n andm, calculate
where 2n = 2·2·...·2 (n factors), and denotes the remainder of division ofx by y.
You are asked to solve the "reverse" problem. Given integers n and m, calculate
The first line contains a single integer n (1 ≤ n ≤ 108).
The second line contains a single integer m (1 ≤ m ≤ 108).
Output a single integer — the value of .
4 42
10
1 58
0
98765432 23456789
23456789
In the first example, the remainder of division of 42 by 24 = 16 is equal to 10.
In the second example, 58 is divisible by 21 = 2 without remainder, and the answer is 0.
这道题当时根本没想那么多,直接输出公式计算结果
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
cout<<(m%(int)(pow(2.0,n*1.0)))<<endl;
return 0;
}
都没有考虑会超出范围,但最后终审也过了。这样写是有问题的,但算是运气好,用错误的方法得出了正确的结果。当pow超出int,是-2147483648,而m的范围不会超出int,在这种情况下结果直接为m。
正确的解法
#include<bits/stdc++.h>
using namespace std;
int n,m;
int main()
{
cin>>n>>m;
cout<<(n<=31?m%(1<<n):m);
}
求余的一些性质:-7%2=-1;7%(-2)=1;
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertexu is called a child of vertex v and vertex v is called a parent of vertexu if there exists a directed edge from v to u. A vertex is called aleaf if it doesn't have children and has a parent.
Let's call a rooted tree a spruce if its every non-leaf vertex has at least3 leaf children. You are given a rooted tree, check whether it's a spruce.
The definition of a rooted tree can be found here.
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the nextn - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of thei + 1-th vertex (1 ≤ pi ≤ i).
Vertex 1 is the root. It's guaranteed that the root has at least2 children.
Print "Yes" if the tree is a spruce and "No" otherwise.
4 1 1 1
Yes
7 1 1 1 2 2 2
No
8 1 1 1 1 3 3 3
Yes
直接将树存在vector中,判断即可。也可以用矩阵相邻法存树。
#include<bits/stdc++.h>
using namespace std;
vector<int>son[1010];
int n;
int main()
{
cin>>n;
int i,j,x;
for(i=2;i<=n;i++)
{
cin>>x;
son[x].push_back(i);
}
int f=0;
for(i=1;i<=n;i++)
{
int cnt=0;
if(son[i].size()!=0)
{
for(j=0;j<son[i].size();j++)
{
int v=son[i][j];
if(son[v].size()==0)
cnt++;
}
if(cnt<3)
{
f=1;
cout<<"No"<<endl;
break;
}
}
if(f)
break;
}
if(!f)
cout<<"Yes"<<endl;
return 0;
}
A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.
Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of typei has volume 2i - 1 liters and costsci roubles. The number of bottles of each type in the store can be considered infinite.
You want to buy at least L liters of lemonade. How many roubles do you have to spend?
The first line contains two integers n andL (1 ≤ n ≤ 30;1 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.
The second line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.
Output a single integer — the smallest number of roubles you have to pay in order to buy at leastL liters of lemonade.
4 12 20 30 70 90
150
4 3 10000 1000 100 10
10
4 3 10 100 1000 10000
30
5 787787787 123456789 234567890 345678901 456789012 987654321
44981600785557577
In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles.
In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles.
In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.
剩下的整整两个小时都在怼C题,感觉很明显的贪心,直接求性价比。if语句比较选择,wa了n次,感觉思路没问题。第二天看别人的代码,发现了一种有意思的解法。第i瓶饮料的体积是2的i-1次方,可直接利用二进制的性质,利用按位运算求解。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,l,ans;
ll a[34];
int main()
{
ans=0;
scanf("%lld%lld",&n,&l);
for(int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(i!=0&&a[i]>2*a[i-1])
a[i]=2*a[i-1];
}
for(int i=n;i<32;i++)
a[i]=2*a[i-1];
for(int i=0;i<32;i++)
{
if(ans>a[i]) ans=a[i];
if(l&(1<<i)) ans+=a[i];
}
printf("%lld\n",ans);
return 0;
}
直接利用性价比计算
#include<bits/stdc++.h>
using namespace std;
struct Node
{
long long v;
long long price;
long double cost;
}node[35];
long long n,l;
bool cmp(Node a,Node b)
{
if(a.cost<b.cost)
return true;
return false;
}
int main()
{
long long i,j;
cin>>n>>l;
for(i=1;i<=n;i++)
{
node[i].v=(long long)pow(2.0,(long double)(i-1));
cin>>node[i].price;
node[i].cost=node[i].price/(node[i].v*1.0);
}
sort(node+1,node+n+1,cmp);
long long sum=0;
long long ans=0x3f3f3f3f3f3f3f3f;
int f=0;
for(i=1;i<=n;i++)
{
long long temp=l/node[i].v;
sum+=temp*node[i].price;
l=l%node[i].v;
if(l==0)
ans=min(ans,sum);
ans=min(ans, sum+node[i].price);;
}
cout<<min(sum,ans)<<endl;
return 0;
}