头像

月亮事务所




离线:21小时前


活动打卡代码 AcWing 2065. 整除序列

#include<iostream>
#include<cstring>

using namespace std;

typedef long long LL ;

int main()
{
    LL n;
    cin>>n;

    while(n)
    {
        cout<<n<<' ';
        n=n/2;
    }
    return 0;
 } 



#include <iostream>
#include <ctype.h>
using namespace std;

string st, sr;

int main()
{
    cin >> st;
    for (int i = 0; i < st.size(); ++i) {
        if (isalpha(st[i])) {
            int x = i; 
            if (i + 1 < st.size() && isdigit(st[i + 1])) {
                ++i;
                if (i + 1 < st.size() && isdigit(st[i + 1])) continue;
                int n = st[i] - '0';
                for (int j = 0; j < n; ++j) sr += st[x];
            }
            else sr += st[x];
        }
    }
    cout << sr << endl;
    return 0;
}





#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;

int n;
int a[N];
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )   scanf("%d", &a[i]);
    sort(a, a + n);

    int d = 0;
    for (int i = 1; i < n; i ++ )
        d = gcd(d, a[i] - a[0]);

    if (!d) printf("%d\n", n);
    else    printf("%d\n", (a[n - 1] - a[0]) / d + 1);

    return 0;
}




#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;
const int N = 1010;
int n;
int g[N];
int f[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&g[i]);
    for(int i=1;i<=n;i++)
    {
        f[i]=1;
        for(int j=1;j<i;j++)
            if(g[j]<g[i])
                f[i] = max(f[i],f[j]+1);
    }
    int res =0;
    for(int i=1;i<=n;i++) res = max(res,f[i]);
    printf("%d\n",res);
    return 0;
}




#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010;
typedef long long ll;
ll a[N];
ll b[N];
int main(){
    int n, k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
    {
        int t;
        cin >> t;
        a[i] = a[i - 1] + t;
    }
    long long ans = 0;
    for(int i = 0; i <= n; i++)
    {
        ll t = a[i] % k ;
        ans += b[t]++;
    }
    cout << ans <<endl;
    return 0; 
}



#include <iostream>

using namespace std;

int k;
string n;

int dfs()
{
    int res = 0;
    while (k < n.size())
    {
        if (n[k] == '(') 
        {
            k ++ ;  
            res += dfs();
            k ++ ;  
        }
        else if (n[k] == '|')
        {
            k ++ ; 
            res = max(res, dfs());
        }
        else if (n[k] == ')') break;
        else
        {
            k ++ ;  
            res ++ ;
        }
    }
    return res;
}
int main()
{
    cin >> n;
    cout << dfs() << endl;
    return 0;
}




#include<iostream>
#include<cstring>
using namespace std;

const int N=40;
int vis[N][N];
int dx[2]={1,0},dy[2]={0,1};
int n,m,ans=0;

void dfs(int x,int y)
{
    if(x==n && y==m)
    {
        ans++;
        return ;
    }

    for(int i=0;i<2;i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(vis[xx][yy]==0  && xx<=n && yy<=m)
        {
            vis[xx][yy]=1;
            dfs(xx,yy);
            vis[xx][yy]=0;
        }
    }
}




#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
constint N 1010;
const int n;
char g[N][N];
bool st[N][N];
PII q[N* N];
int dx[4]={-1,0,1,0}
int dy[4]={0,1,0,-1};
void bfs(int sx, int sy, int &total, int &bound)
{
int hh = 0, tt = 0;
q[0]={sx,sy};
st[sx][sy] = true;
while (hh <= tt)
{
PII t=q[hh ++];
total ++;
bool is_bound=false;
for (int i=0;i < 4;i ++)
int x=t.x+dx[i],y =t.y+dy[i];
if (x< 0 ||x>=n||y<0 ||y >= n) continue; 
if(st[x][y]) continue;
if(g[x][y] =='.')
{
is _bound = true;
continue;
}
q[++ tt]= {x,y};
st[×][y]= true;
}
if(is_bound) bound ++;
}
int main()
{
scanf("%d",&n);
for (int i=0;i< n;i++)scanf("%s",g[i]);
int cnt=0;
for (int i =0;i<n;i++)
    for(int j = 0;j<n;j ++)
        if(!st[i][j]&& g[i][j]==' #')
        {
            int total = 0, bound=0;
            bfs(i, j, total,bound);
            if (total == bound) cnt ++;
        }
        printf("%d\n",cnt);
    return 0;
}




#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;
const int N=10010;
int n,m;
int q[N];
int r,x,l;

int main()
{
scanf("%d%d",&n,&m); 
for(int i=0;i< n; i ++) scanf("%d",&q[i]);
for (int i= 0; i<m;i++)
{
int x;
scanf("%d",&x);
int l=0,r=n-1; 
while(l<r)
int mid =1+r>>1;
if (q[mid] >=x) r=mid;
else l= mid + 1;
}
if(q[r] == x)
{
cout<<r<<' ';
r=n-1;
while (l<r)
{
    int mid=1+r>>1;
    if(q[mid]<=x) l=mid;
    else r=mid-1;
}
cont<<l<<endl;
}
else cout<<"-1 -1"<<endl;
}
return 0;




#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int w, m, n;
cin >> w >>m>> n;
m--,n-- ;
int x1= m%w,x2=n/ w;
int y1=m % w, y2 =n%w;
if(x1%2)    y1 = w -1- y1;
if(x2%2)   y2= w- 1 -y2;
cout <<abs(x1 -x2)+ abs(y1 - y2) <<endl;
return 0;
}