暑假训练赛7

Problem A: 2^x mod n = 1

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 587  Solved: 244

[Submit][Status][Web Board]

Description

Give a number n, find the minimum x that satisfies 2^x mod n = 1.

Input

One positive integer on each line, the value of n.

Output

If the minimum x exists, print a line with 2^x mod n = 1.

Print 2^? mod n = 1 otherwise.

You should replace x and n with specific numbers.

Sample Input

2 5

Sample Output

2^? mod 2 = 1 2^4 mod 5 = 1

【分析】循环就好。注意累乘的时候会由于数太大溢出,所以要处理一下,模运算。

           (a * b) % p = (a%p)*(b%p) %p;下面用的时候最后一个取余运算没有做是因为就是要求余1的时候,倒数第二次已经是1了,再与其他的取余还是1,所以会多一次,所以就不用再算;

#include<bits/stdc++.h>
using namespace std;
int main()
{
   long long n;
   while(~scanf("%lld",&n))
   {
       int flag=0;
       if(n%2==0||n==1)            //2的倍数肯定会被2整除,1也肯定不是
       {
           printf("2^? mod %d = 1\n",n);
           continue;
       }
        int w=2,j=1;  
       while(w!=1)  
       {  
           j++;  
           w *= 2;      //每乘一个2就对2取余,这样对结果没有影响,不知道为什么;但是如果这里不是对每个取余而是对累乘的结果取余的话就会超时
                              // (a * b) % p = (a%p)*(b%p) %p
           w %= n;
       } 
       printf("2^%d mod %d = 1\n",j,n);  
   }
   return 0;
}

 

Problem B: 又是比智力

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 1705  Solved: 325

[Submit][Status][Web Board]

Description

松哥上了数学课之后,觉得自己智力实在有所不足,所以他决定找人辩论,以提高自己的智力,已知松哥目前的智力是m,他决定和n个人辩论,如果他对手的智力低于他,松哥的智力能够提升2,否则只能提升1,假设松哥能够取得所有的胜利,请问他完成n场辩论后能够得到的最高智力是多少?

Input

多组测试数据.

每组测试数据的第一行包含两个正整数m,n.(m<=100,n<=10^5)

第二行为n个不大于100的整数,代表与他辩论人的智力.

Output

对于每组测试数据,他完成n场辩论后,能取得的最大的智力.

Sample Input

91 5 88 90 92 94 98

Sample Output

101

 

【分析】

  •     数组要开得大一点!!!!!!!这个错误我也是很绝望了↓↓↓

        A Not allowed system call: runid:382659 :使用了系统禁止的操作系统调用,看看是否越权访问了文件或进程等资源

  • 注意不只是排序,有点像博弈,就是如果比这个小的话你就去找最大的来比,使得到的分数尽可能多

#include<bits/stdc++.h>
using namespace std;
int a[11000];
int main()
{
   int m,n;
   while(~scanf("%d%d",&m,&n))
   {
       memset(a,0,sizeof(a));
       for(int i=0;i<n;i++)
           scanf("%d",&a[i]);
       sort(a,a+n);
       for(int i=0;i<n;i++)
       {
           if(a[i]<m)m+=2;
           else {
               i--;n--;
               m++;    
           }
       //cout<<m<<endl;
       }    
       cout<<m<<endl;
   }
   return 0;
}

 

Problem C: Chips

Time Limit: 2 Sec  Memory Limit: 256 MB

Submit: 42  Solved: 29

[Submit][Status][Web Board]

Description

There are n walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the walrus number 2, ..., the walrus number 1 sits to the left of the walrus number n.

The presenter has m chips. The presenter stands in the middle of the circle and starts giving the chips to the walruses starting from walrus number 1 and moving clockwise. The walrus number i gets i chips. If the presenter can't give the current walrus the required number of chips, then the presenter takes the remaining chips and the process ends. Determine by the given n and m how many chips the presenter will get in the end.

Input

The first line contains two integers n and m (1≤n≤50, 1≤m≤104) − the number of walruses and the number of chips correspondingly.

Output

Print the number of chips the presenter ended up with.

Examples

Input

4 11

Output

0

Input

17 107

Output

2

Input

3 8

Output

1

Note

In the first sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, four chips to the walrus number 4, then again one chip to the walrus number 1. After that the presenter runs out of chips. He can't give anything to the walrus number 2 and the process finishes.

In the third sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, then again one chip to the walrus number 1. The presenter has one chip left and he can't give two chips to the walrus number 2, that's why the presenter takes the last chip.

【分析】

  • 大致题意就是,给编号1~n的分薯条,每个人每次获得编号数目的薯条,一共有m个,问最后还剩余多少

  • 如果薯条总数小于下一个的编号数,则无法继续,此刻持有的薯条数就是剩下的;而如果下一个的编号大于n,那么就可以循环,从编号1的再来一次。

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n,m;
   while(~scanf("%d%d",&n,&m))
   {
       int i=1;
       while(1)
       {
           if(m-i>=0)m-=i;
           else {
               printf("%d\n",m);
               break;
           }
           i++;
           if(i==n+1)i=1;
           //cout<<i<<endl;
       }
   }
   return 0;
}

 

Problem D: 新年彩灯Ⅰ

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 518  Solved: 72

[Submit][Status][Web Board]

Description

新年将至,YY准备挂一排彩灯,已知彩灯刚挂完的彩灯共有N盏(编号为1,2,3,……),并且都是灭的。彩灯的闪烁由一段程序控制。

每一秒钟程序会生成两个正整数a和b(1<=a,b<=N),然后将编号为a和b之间的所有灯的状态改变一次,即如果灯i是灭的,那么经过一次改变,灯i会亮,如果灯i是亮的,经过一次改变,灯i会灭。

当YY看着自己挂的彩灯不断闪烁的时候,问题来了,YY想知道任意时刻某一区间灯的状态。

Input

多组测试数据,每一组第一行是一个整数N(1<=N<=1000000)和一个整数M(1<=M<=3000)。

然后是M行数据,包括以下两种形式:

        1 a b 表示灯a和灯b之间的灯(含灯a和灯b)变换一次状态。

        0 x y 表示YY想知道此刻灯x到灯y(包含灯x和灯y)的状态.

Output

对于每次YY想知道结果的时候,输出一行灯的状态(编号小的灯优先),如果是亮的输出”1”,否则输出”0”;

Sample Input

3 3 1 1 2 1 2 3 0 1 3

Sample Output

101

【分析】

       emmm线段树,树状数组,我还没看会。。。实在搞不清Lowbit到底想干嘛,,先放个链接好了

           https://blog.csdn.net/x948675238/article/details/81275000

 

 

Problem E: Binary Number

Time Limit: 2 Sec  Memory Limit: 256 MB

Submit: 56  Solved: 29

[Submit][Status][Web Board]

Description

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Little walrus Fangy loves math very much. That's why when he is bored he plays with a number performing some operations.

Fangy takes some positive integer x and wants to get a number one from it. While x is not equal to 1, Fangy repeats the following action: if x is odd, then he adds 1 to it, otherwise he divides x by 2. Fangy knows that for any positive integer number the process ends in finite time.

How many actions should Fangy perform to get a number one from number x?

Input

The first line contains a positive integer x in a binary system. It is guaranteed that the first digit of x is different from a zero and the number of its digits does not exceed 106.

Output

Print the required number of actions.

Examples

Input

1

Output

0

Input

1001001

Output

12

Input

101110

Output

8

Note

Let's consider the third sample. Number 101110 is even, which means that we should divide it by 2. After the dividing Fangy gets an odd number 10111 and adds one to it. Number 11000 can be divided by 2 three times in a row and get number 11. All that's left is to increase the number by one (we get 100), and then divide it by 2 two times in a row. As a result, we get 1.

【分析】

  1. 就是二进制的理解问题吧。如果是奇数则末尾必然是1,+1的话就是把改为变成该位变为0,把上一位变为1;如果是偶数的话末尾一定为0,就是把所有的位后移一位也就是把末尾的0去掉;

  2. 关键在奇数的处理吧,要注意i,j在作为实参传入,但函数调用结束后i和j的值并没有发生改变;

#include<bits/stdc++.h>
using namespace std;
char s[1000010];
int ans;
void ff(int l,int r)
{
    int f=1;
    while(r>l)
    {
        if(s[r]=='1')
        {
            s[r]='0';
            r--;
        }
        else
        {
            s[r]='1';
            f=0;
            break;
        }
    }
    if(f && s[l]=='1')ans++;
}
int main()
{
//    memset(s,'\0',sizeof(s));
    while(~scanf("%s",s))
    {
        int l=strlen(s);
        int i=0,j=l-1;
        ans=0;
        while(i<j)
        {
            ans++;
           if(s[j]=='0')
                j--;
            else ff(i,j);
        }
        cout<<ans<<endl;
    }
}

 

Problem K: Frames

Time Limit: 2 Sec  Memory Limit: 256 MB

Submit: 63  Solved: 25

[Submit][Status][Web Board]

Description

Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well.

This time Igor K. got disappointed in one of his hobbies: editing and voicing videos. Moreover, he got disappointed in it so much, that he decided to destroy his secret archive for good.

Igor K. use Pindows XR operation system which represents files and folders by small icons. At that, m icons can fit in a horizontal row in any window.

Igor K.'s computer contains n folders in the D: disk's root catalog. The folders are numbered from 1 to n in the order from the left to the right and from top to bottom (see the images). At that the folders with secret videos have numbers from a to b inclusive. Igor K. wants to delete them forever, at that making as few frame selections as possible, and then pressing Shift+Delete exactly once. What is the minimum number of times Igor K. will have to select the folder in order to select folders from a to b and only them? Let us note that if some selected folder is selected repeatedly, then it is deselected. Each selection possesses the shape of some rectangle with sides parallel to the screen's borders.

Input

The only line contains four integers n, m, a, b (1≤n,m≤109, 1≤a≤b≤n). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted.

Output

Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from a to b.

Examples

Input

11 4 3 9

Output

3

Input

20 5 2 20

Output

2

Note

The images below illustrate statement tests.

The first test:

In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection.

The second test:

In this test we can first select all folders in the first row (2, 3, 4, 5), then − all other ones.

 

【分析】要考虑很多很多种情况

  • m=1时显然次数最小值为1.

  • b=n时特判:如果a在第一列或者a,b同行,次数为1,否则次数为2

  • 所有a在第1列,b在最后一列的情况下或a,b同行,最小次数都是1.

  • a在第一列或b在最后一列或者a,b的列相邻(对角的两个矩形)或者a,b所在行相邻则次数为2

  • 剩下的次数都是3.

#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n,m,a,b,ans;
   while(~scanf("%d%d%d%d",&n,&m,&a,&b))
   {
       int st=a/m+(a%m!=0);
       int ed=b/m+(b%m!=0);
       if(m==1)ans=1;
       else if(b==n){
           if(a%m==1||st==ed)ans=1;
           else ans=2;
       }
       else if((a%m==1&&b%m==0)||st==ed)ans==1;
       else if(a%m==1||b%m==0||st+1==ed||(b+1)%m==a%m)ans=2;
       else ans=3;
       cout<<ans<<endl;
   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值