Zhongshan Day5-- popularity

Today the topic is really difficult na thief. . . Only 38. . .

Harvest: Fenwick tree single modification

           Fenwick tree amend section


 

T1: Travel

Meaning of the questions: There are n numbers, ask; to reap any number, their programs and the number of prime numbers is how much? (N <= 50)

Violence can be simulated here do not speak.

See Code:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n,ans,sum,a[551],b[551];
bool flag[551][10001];
bool pan(int x)
{
    if(x==1)
    return false;
    int i=2;
    while(i<=sqrt(x))
    {
        if(x%i==0)
        break;
        else
        i++;
    }
    if(i>sqrt(x))
    return true;
    else
    return false;
}
void dfs(int x)
{
    for(int i=b[x-1]+1;i<=n;i++)
    {
        if(flag[x][a[i]]==false)
        {
            sum+=a[i];
            flag[x][a[i]]=true;
            b[x]=i;
            if(pan(sum))
            {
                ans++;
            }
            dfs(x+1);
            b[x]=0;
            sum-=a[i];
        }
    }
}
bool cmp(int x,int y)
{
    return x<y;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+n+1,cmp);
    dfs(1);
    printf("%d",ans);
    return 0;
}

Good question indeed! ! !


T2: Mysterious Hills

 See questions:

Tsui Hang Village is a mysterious Villa, not because it gave birth to the great man Sun Yat-sen, is more mysterious ghost Hill, there are only N, M rabbits, of course, you. Where every second:

 

1. Exactly two creatures encountered.

 

2. The probability of encounter between any two organisms are equal.

 

If two rabbits meet, nothing happens; if two ghosts meet, they will tussle with each other, eventually die together; If you encounter a ghost rabbit, the rabbit will be eaten; if you encounter a ghost, ha ha. . . . symbol to see you; if you come across a rabbit, then you can choose to kill or not to kill (equal probability).

 

Q. Can you live to see the probability of the symbol.
 
Obviously: Only ghost die off in order to win. So what are the rabbit is not, and the rest, if the ghost is odd, responsible for certain death. If the ghost is even, because the last people only live a ghost, so the probability is 1 / (n + 1).
See Code:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double n,m,a,sum=1;
int main()
{
    scanf("%lf%lf",&n,&m);
    a=m+1+n;
    if(int(n)%2==0)
    printf("%.6lf",1/(n+1));
    else
    printf("%.6lf",0);
    return 0;
}

Good question indeed! ! !


T3: Lucky lock

Fortunately, there is a lock to open it will bring you good luck, but unlock required to enter a positive integer (no leading 0). Fortunately, there is a lock operation, for a positive integer, returns the difference between two adjacent digital his, such as 1135, the operation result is 22 (will remove the leading 0)

Now known only after repeated operation to open the final result is the number to 7 of the lock, give you an interval [a, b], ask how many locks can be opened lucky lucky number in this interval there. Limitations [1 <= a <= b < = 10 ^ 9.
Ideas: violence hit the table, violence enumeration. (Chiefs are said to have hit the table with AC pascal)

See Code:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[101],b[101],flag,head;
bool pan(int x)
{
    flag=0;
    while(x!=0)
    {    
        flag++;
        b[flag]=x%10;
        x/=10;
    }
    for(int i=1;i<=flag;i++)
    a[i]=b[flag-i+1];
    while(flag>1)
    {
        head=1;
        while(a[1]==0)
        {
            for(int j=2;j<=flag;j++)
            {
                a[j-1]=a[j];
            } 
            flag-=1;
            if(flag==1)
            break;
        }
        for(int i=1;i<flag;i++)
        {
            a[head]=abs(a[i]-a[i+1]);
            head++;
            for(int j=i+2;j<=flag;j++)
            {
                a[j-1]=a[j];
            }    
            flag--;
        }
    }
    if(a[1]==7)
    return true;
    return false;
}
int main()
{
    freopen("lucky.in","r",stdin);
    freopen("lucky.out","w",stdout);
    int a1,b1,j,j1=0,ans=0;
    scanf("%d%d",&a1,&b1);
    for(int j=a1;j<=b1;j++)
    if(pan(j)==true)
    ans++; 
    printf("%d\n",ans);
    return 0;
}

Good question indeed! ! !


T4: Simple question

The meaning of problems: Given a sequence, there are two modes of operation: a digital interrogation zone and over a period, for each of a number of the interval plus a value. You need to answer every inquiry.

Ideas: a look at this question: Yo Oh! Not the tree line interval modified template do and seconds do say.

See Code:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long n,q,d,s,c1[100001],sum1[100001],c2[100001],a[100001];
char w;
long long lowbit(long long x)
{
    return x&(-x);
}
long long sum(long long c[],long long x)
{
    long long ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
void add(long long c[],long long x,long long y)
{
    while(x<=n)
    {
        c[x]+=y;
        x+=lowbit(x);
    }
}
int main()
{
    freopen("simple.in","r",stdin);
    freopen("simple.out","w",stdout);
    scanf("%ld%ld",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
        sum1[i]=sum1[i-1]+a[i];
    }
    for(int j=1;j<=q;j++)
    {
        cin>>w;
        scanf("%ld%ld",&s,&d);
        if(int(w)==int('Q'))
        {
            long long suml=sum1[s-1]+s*sum(c1,s-1)-sum(c2,s-1);
            long long sumd=sum1[d]+(d+1)*sum(c1,d)-sum(c2,d);
            printf("%ld\n",sumd-suml); 
        }
        else 
        {
            long long cost;
            scanf("%ld",&cost);
            add(c1,s,cost);
            add(c1,d+1,-cost);
            add(c2,s,s*cost);
            add(c2,d+1,-(d+1)*cost);
        }
    }
    return 0;
}

Good question indeed! ! !

 

Guess you like

Origin www.cnblogs.com/qing1/p/11305218.html