地址:传送门
1001 Cut The Wire
题目:
题意:
有从1开始无穷多个数,这些数之间的连线规则如下:
对于x:
1)与x/2连线
2)与3*x+1连线
问让n到n+1没有任何线连接要减多少条线?
题解:
对规则分析
1)反着推,就是每一个数都和他的2倍连接的意思,只要它的2倍大于n,那就肯定要剪这条线。其实就是取n/2,如果是奇数还要再+1~
2)同理,如果这个数*3+1大于n就要减去。这个操作琐碎一点。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+7;
const int mod=1e9+7;
int main()
{
int t;
cin>>t;
while(t--)
{
ll n,ans=0,t=0;
cin>>n;
ans=n-n/2;
ll x=(n+1)/3;
if(x%2==0) x++;
if(x*3+1==n) t=(n+1)/2-(x+1)/2;
else t=(n+1)/2-(x+1)/2+1;
ans+=t;
cout<<ans<<endl;
}
return 0;
}
1006 Power Sum
题目:
题意:
找出一个数,它满足
题解:
目标——消去i使得某段数组无论在哪个地方的和为固定的数值。
公式:[n2 -(n-1)2 ]+[ (n-3)2-(n-2)2 ]=4;
所以表示一个数只需要表示4的余数剩下的用4来填补就行啦。
1可以表示:1
2可以表示:0001
3可以表示:01
4可以表示:1001
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
const int mod=1e9+7;
#define mk make_pair
typedef long long ll;
int main(){
int t;
cin>>t;
while(t--){
int n;
scanf("%d",&n);
if(n==1){
cout<<1<<"\n"<<1<<"\n";
}else if(n==2){
cout<<4<<"\n"<<"0001\n";
}else if(n==3){
cout<<2<<"\n"<<"01\n";
}else {
if(n%4==0){
cout<<n<<"\n";
for(int i=0;i<n/4;i++){
cout<<"1001";
}cout<<"\n";
}
else {
if(n%4==1){
cout<<n/4*4+1<<"\n";
cout<<1;
for(int i=0;i<n/4;i++){
cout<<"1001";
}cout<<"\n";
}if(n%4==2){
cout<<n/4*4+4<<"\n";
cout<<"0001";
for(int i=0;i<n/4;i++){
cout<<"1001";
}cout<<"\n";
}if(n%4==3){
cout<<n/4*4+2<<"\n";
cout<<"01";
for(int i=0;i<n/4;i++){
cout<<"1001";
}cout<<"\n";
}
}
}
}
}
Command Sequence
题目:
题意:
四个字母表示四个方向,问有多少个组字符串从某一位置出发再回到某位置。
题解:
如果从某位置回到某位置意味着什么?
向上次数=向下次数,向左次数=向右次数。
那么,如果设定初始位置为(0,0),向上++,向下- -,向左++,向右- -。
那么满足条件的初始末位置肯定是相同的。统计有多少个这样的数对。每一个数对可以组合(个数为n):n*(n-1)/2
可以用map存哈~
#include<bits/stdc++.h>
using namespace std;
#define mk make_pair
int main(){
int n;
int t;
cin>>t;
while(t--){
int n;
map<pair<int,int>,int> mp;
cin>>n;
string s;
cin>>s;
mp[mk(0,0)]++;
int x=0,y=0;
for(int i=0;i<n;i++){
if(s[i]=='U'){
y++;
}else if(s[i]=='D'){
y--;
}else if(s[i]=='L'){
x--;
}else {
x++;
}mp[mk(x,y)]++;
}long long ans=0;
map<pair<int,int>,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
long long k=it->second;
if(k>=2){
ans+=k*(k-1)/2;
}
}cout<<ans<<"\n";
}
}
*这场一直在躺,全为队友代码