C++中 ++i与i++的效率比较

Problem B
Super Number
Input: 
Standard Input

Output: Standard Output

Time Limit: 3 Seconds

 

Don‘t you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a | b means ‘b is divisible by a’)

C++中 ++i与i++的效率比较
Figure: Super Numbers

 

Given n, m (0 < n < m < 30), you are to find a m-digit positive integer X such that for every i (n <= i <= m), the first i digits of X is a multiple ofi. If more than one such X exists, you should output the lexicographically smallest one. Note that the first digit of X should not be 0.

 

Input 

The first line of the input contains a single integer t(1 <= t <= 15), the number of test cases followed. For each case, two integers n and m are separated by a single space.

 

Output 

For each test case, print the case number and X. If no such number, print -1.

 

Sample Input                           Output for Sample Input

2

1 10

3 29

Case 1: 1020005640

Case 2: -1

 


Problemsetter: Rujia Liu, Member of Elite Problemsetters‘ Panel

Special Thanks to:

Monirul Hasan (Alternate solution)

Shahriar Manzoor (Figure Drawing)


题意:给定两个数字n,m;求一个长度为m的数字使得这个数字的任意前i(n=<i<=m)个数字组成的数能被i整数。

这道题按照我的思路就是回溯暴力。暂时没想到更好的方法了。

#include<iostream>
#include<cstring>

using namespace std;

int n,m,tag;
int arry[50];

int mod(int cnt)
{
    int sum=0;
    for(int i=0;i<cnt;i++)
    {
        sum=(sum*10+arry[i])%cnt;
    }
    return sum;
}

void dfs(int pos)
{
    if(pos==m)
    {
        tag=1;
        return;
    }
    if(tag) return;
    for(int i=0;i<=9;i++)
    {
        arry[pos]=i;
        if(pos<n-1||(pos>=n-1&&!mod(pos+1)))
        {
            dfs(pos+1);
            if(tag) return;
        }
    }
}

int main()
{
    int k;
    cin>>k;
    for(int t=1;t<=k;t++)
    {
        tag=0;
        cin>>n>>m;
        memset(arry,0,sizeof(arry));
        int i,j;
        for(i=1;i<=9;i++)
        {
            arry[0]=i;
            dfs(1);
            if(tag) break;
        }
        cout<<"Case "<<t<<": ";
        if(tag)
        {
            for(i=0;i<m;i++) cout<<arry[i];
        }
        else cout<<"-1";
        cout<<endl;
    }
    return 0;
}


C++中 ++i与i++的效率比较

上一篇:C++赋值操作符与析构函数


下一篇:Python模块整理(三):子进程模块subprocess