Bzoj4724 -- number theory, bzoj Number Theory

Source: Internet
Author: User

Bzoj4724 -- number theory, bzoj Number Theory

Question:

B In number, each number I (I =,..., B-1) has a [I. You need to use these numbers to form the largest number X in B (cannot have leading zero, do not need to use all numbers), so that X is a multiple of the B-1. Q: What is the k-bit number of X in the B-base system? The second digit is 0th bits ). Ideas:
Due to the following theorem:
A * Bk rating a (mod (B-1 ))So as long as the sum of all BITs is a multiple of the B-1. Note that a [I]> = 1, just delete sum % (B-1. The question is divided into two parts. Code:
 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define ll long long 6 char buf[100000],*p1=buf,*p2=buf; 7 inline char Nc(){ 8     if(p1==p2)p2=(p1=buf)+fread(buf,1,100000,stdin); 9     return *p1++;10 }11 inline void Read(ll& x){12     char c=Nc();13     for(;c<'0'||c>'9';c=Nc());14     for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=Nc());15 }16 inline void Read(int& x){17     char c=Nc();18     for(;c<'0'||c>'9';c=Nc());19     for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=Nc());20 }21 int i,j,n,m;22 ll a[1000001],k,Sum;23 inline int Find(ll k){24     int l=0,r=n-1,Mid;25     while(l<=r){26         Mid=l+r>>1;27         if(a[Mid]>=k)r=Mid-1;else l=Mid+1;28     }29     return l;30 }31 char s[20];32 int Len;33 inline void Print(int x){34     if(x==0){35         puts("0");36         return;37     }38     for(Len=0;x;x/=10)s[++Len]=x%10;39     for(;Len;)putchar(s[Len--]+48);40     puts("");41 }42 int main()43 {44     Read(n);Read(m);45     for(i=0;i<n;i++)Read(a[i]),Sum=Sum+a[i]*i;46     if(Sum%(n-1))a[Sum%(n-1)]--;47     for(i=1;i<n;i++)a[i]+=a[i-1];48     while(m--){49         Read(k);50         if(++k>a[n-1])puts("-1");else Print(Find(k));51     }52 }
Bzoj4724

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.