E - Integer Divisibility

 

If an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.

For example you have to find a multiple of 3 which contains only 1's. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3's then, the result is 6, because 333333 is divisible by 7.

Input
Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case will contain two integers n (0 < n ≤ 106 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).

Output
For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.

Sample Input
3
3 1
7 3
9901 1

Sample Output
Case 1: 3
Case 2: 6
Case 3: 12

 

翻译

如果整数不能被2或5整除,则十进制表示法中该数字的某个倍数只是一个数字的序列。现在您将获得数字和唯一允许的数字,您应该报告此数字的位数。
例如,您必须找到仅包含1的3的倍数。然后结果是3,因为111(3位)可被3整除。同样,如果你发现7的多数只包含3的那么,结果是6,因为333333可以被7整除。


输入:

输入以整数开始T(≤300),表示测试用例的数量。

每种情况将包含两个整数n(0 <n≤106且n不能被2或5整除)和允许的数字(1≤dig≤9)。

输出:

对于每种情况,打印案例编号和此类倍数的位数。如果有几种解决方案; 报告最小的一个。

样例输入:

3
3 1
7 3
9901 1

样例输出:

案例1:3
案例2:6
案例3:12

 

 

解决本题的方法:大数取模

一个大数对一个数取余,可以把大数看成各位数的权值与个位数乘积的和。

比如1234 = ((1 * 10 + 2) * 10 + 3) * 10 + 4,对这个数进行取余运算就是上面基本加和乘的应用。

代码实现:

char s[MAX];
int len = strlen(s);
int ans = 0;
for (int i = 0; i < len; i ++)
{
    ans = (ans * 10 + a[i] - '0') mod b;
}

 

这里只需要将循环条件、 a[i] - '0'  改一下(因为循环次数不清楚)

 

 

最初想法很简单循环算( n * 10 + n ),然后对m取余等于零停止,输出循环次数即可,但是Time Limit,后来想用快速幂依次算出n^1 n^2 n^3 ... 然后各项对m取余再累加求和的,但是Runtime Error,最后想起来大数求模,最终AC。。。。。

 

Time Limit代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ll i,t,m,n,flag,ans1,ans;
    scanf("%lld", &t);
    for(i=0; i < t; i++)
    {
        scanf("%lld%lld", &n, &m);
        flag = m;
        ans=0;
        ans1 = 0;
        while(m %= n)
        {
            ans1 = (ans1 * 10 + flag);
            m=ans1;
            ans++;
        }
        printf("Case %lld: %lld\n",i+1,ans);
    }
    return 0;
}

 

 Runtime Error代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int m,n;

ll quick_pow(ll a,ll b)
{
    ll ret = 1;
    while(b)
    {
        if(b & 1) ret = ret *a % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return ret;

}

int main()
{
    ll i,t,flag,b,ans1,ans;
    scanf("%lld", &t);
    for(i=0; i < t; i++)
    {
        scanf("%lld%lld", &m, &n);
        ans = n;    b=1;
        while(ans)
        {
            ans = ((ans % m) + n * quick_pow(10,b)) % m;
            b++;
        }
        printf("Case %lld: %lld\n",i+1,b);
    }
    return 0;
}

 

最后附上AC代码。。。。。

AC代码:

 

自我感觉代码过于复杂,但思路还是挺明确的。。。。。。。好吧,可能是我自我感觉@(&^...^&)@!!!!!!

 

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/********本来是想用快速幂依次算出n^1 n^2 n^3 ... 然后各项对m取余再累加求和的,
                但是最后想起来大数求模******/
/*
ll quick_pow(ll a,ll b)
{
    ll ret = 1;
    while(b)
    {
        if(b & 1) ret = ret *a;
        a = (a * a);
        b >>= 1;
    }
    return ret;

}*/

int main()
{
    ll i,t,m,n,flag,b,ans1,ans;
    scanf("%lld", &t);
    for(i=0; i < t; i++)
    {
        scanf("%lld%lld", &m, &n);
        flag = n;
        ans = 0;
//注意ans1是从0开始的,因为如果ans1从1开始取的话ans1 != (ans1 * 10 +flag) % m;
//自己举个例子就明白了
        ans1 = 0;  b = 1;
//循环求最小的满足(n % m == 0)的ans
        while(b)
        {
            ans1 = (ans1 * 10 + flag) % m;
            b = ans1;
            ans++;

        }
        printf("Case %lld: %lld\n",i+1,ans);
    }
    return 0;
}

 

最后的最后来一碗鸡汤:

        我想说的是一定不要放弃,尽管你可能一题都没做出来,别人已经全AC了,但那又怎样,一定要自己先思考尝试解决,实在解决不了,搜搜别人博客,先看看别人的思路,自己再从别人的思路去考虑,尝试解题,还做不出来,再去借鉴别人的代码,注意:一定要弄懂别人的代码,下次出类似的题要能自己会做,自己会做才是关键!!!!!!

 

 

 

 

[71]"D:/Work/CCS/ccs_20/ccs/tools/compiler/ti-cgt-armllvm_4.0.3.LTS/bin/tiarmclang.exe" -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -mlittle-endian -mthumb -gdwarf-3 -march=armv7e-m -Wl,-m"simple_tx_rfprop.map" -Wl,-i"D:/Work/SatIoT/CC1352/SDK/simplelink_cc13xx_cc26xx_sdk_8_30_01_01/source" -Wl,-i"D:/Work/SatIoT/CC1352/SDK/simplelink_cc13xx_cc26xx_sdk_8_30_01_01/kernel/tirtos7/packages" -Wl,-i"D:/Work/Project/jiang/bload-master/simple_tx_rfprop/Debug/syscfg" -Wl,-i"D:/Work/CCS/ccs_20/ccs/tools/compiler/ti-cgt-armllvm_4.0.3.LTS/lib" -Wl,--diag_wrap=off -Wl,--display_error_number -Wl,--warn_sections -Wl,--xml_link_info="simple_tx_rfprop_linkInfo.xml" -Wl,--rom_model -o "simple_tx_rfprop.out" "./main_tirtos.o" "./rfPacketTx.o" "./syscfg/ti_devices_config.o" "./syscfg/ti_radio_config.o" "./syscfg/ti_drivers_config.o" "./syscfg/ti_sysbios_config.o" "./oad/bim_interface.o" "./oad/flash_interface.o" "./oad/oad_image_header.o" "./oad/upgrade_manager.o" "../cc13x2_cc26x2_tirtos7_active.cmd" -Wl,-lti_utils_build_linker.cmd.genlibs -Wl,-llibc.a [72]gmake[1]: *** No rule to make target 'input-file.error', needed by 'output-file.error'. [73]Finished building target: "simple_tx_rfprop.out" [74]D:/Work/CCS/ccs_20/ccs/tools/compiler/ti-cgt-armllvm_4.0.3.LTS/bin/tiarmobjcopy -O ihex simple_tx_rfprop.out simple_tx_rfprop.hexD:/Work/SatIoT/CC1352/SDK/simplelink_cc13xx_cc26xx_sdk_8_30_01_01/tools/common/oad/oad_image_tool --verbose ccs D:/Work/Project/jiang/bload-master/simple_tx_rfprop/Debug 7 -hex1 D:/Work/Project/jiang/bload-master/simple_tx_rfprop/Debug/simple_tx_rfprop.hex -k D:/Work/SatIoT/CC1352/SDK/simplelink_cc13xx_cc26xx_sdk_8_30_01_01/tools/common/oad/private.pem -o D:/Work/Project/jiang/bload-master/simple_tx_rfprop/Debug/simple_tx_rfprop_oad [75]OVERVIEW: llvm-objcopy tool [76]USAGE: llvm-objcopy [options] input [output] [77]OPTIONS: [78] --add-gnu-debuglink=debug-file [79] Add a .gnu_debuglink for <debug-file> [80] --add-note-segment=file Make a note segment with the contents of <file>. [81] --add-section=section=file [82] Make a section named <section> with the contents of <file> [83] --add-symbol=name=[section:]value[,flags] [84] Add new symbol <name> to .symtab. Accepted flags: global, local, weak, default, hidden, protected, file, section, object, function, indirect-function. Accepted but ignored for compatibility: debug, constructor, warning, indirect, synthetic, unique-object, before [85] --adjust-start <value> Alias for --change-start [86] --allow-broken-links Allow the tool to remove sections even if it would leave invalid section references. The appropriate sh_link fields will be set to zero. [87] --binary-architecture=<value> [88] Ignored for compatibility [89] -B <value> Alias for --binary-architecture [90] --change-start=incr Add <incr> to the start address. Can be specified multiple times, all values will be applied cumulatively [91] --compress-debug-sections=format [92] Compress DWARF debug sections using specified format. Supported formats: zlib, zstd. Select zlib if <format> is omitted [93] --core-exe=coreid:filename [94] Specify an ELF executable for a Core ID as input. [95] --decompress-debug-sections [96] Decompress DWARF debug sections [97] --disable-deterministic-archives [98] Disable deterministic mode when operating on archives (use real values for UIDs, GIDs, and timestamps). [99] --discard-all Remove all local symbols except file and section symbols. Also remove all debug sections [100] --discard-locals Remove compiler-generated local symbols, (e.g. symbols starting with .L) [101] --dump-mmELFConfig Dump the contents of the internal mmELFConfig structure. [102] --dump-section=section=file [103] Dump contents of section named <section> into file <file> [104] -D Alias for --enable-deterministic-archives [105] --enable-deterministic-archives [106] Enable deterministic mode when operating on archives (use zero for UIDs, GIDs, and timestamps). [107] --extract-dwo Remove all sections that are not DWARF .dwo sections from file [108] --extract-main-partition [109] Extract main partition from the input file [110] --extract-partition=name [111] Extract named partition from input file [112] -F <value> Alias for --target [113] --gap-fill=value Fill the gaps between sections with <value> instead of zero. <value> must be an unsigned 8-bit integer. This option is only supported for ELF input and binary output [114] --globalize-symbol=symbol [115] Mark <symbol> as global [116] --globalize-symbols=filename [117] Reads a list of symbols from <filename> and marks them global [118] -G <value> Alias for --keep-global-symbol [119] -g Alias for --strip-debug [120] --input-target=<value> Format of the input file [121] -I <value> Alias for --input-target [122] -j <value> Alias for --only-section [123] --keep-file-symbols Do not remove file symbols [124] --keep-global-symbol=symbol [125] Convert all symbols except <symbol> to local. May be repeated to convert all except a set of symbols to local [126] --keep-global-symbols=filename [127] Reads a list of symbols from <filename> and runs as if --keep-global-symbol=<symbol> is set for each one. <filename> contains one symbol per line and may contain comments beginning with '#'. Leading and trailing whitespace is stripped from each line. May be repeated to read symbols from many files [128] --keep-section=section Keep <section> [129] --keep-symbol=symbol Do not remove symbol <symbol> [130] --keep-symbols=filename Reads a list of symbols from <filename> and runs as if --keep-symbol=<symbol> is set for each one. <filename> contains one symbol per line and may contain comments beginning with '#'. Leading and trailing whitespace is stripped from each line. May be repeated to read symbols from many files [131] --keep-undefined Do not remove undefined symbols [132] -K <value> Alias for --keep-symbol [133] --localize-hidden Mark all symbols that have hidden or internal visibility as local [134] --localize-symbol=symbol [135] Mark <symbol> as local [136] --localize-symbols=filename [137] Reads a list of symbols from <filename> and marks them local [138] -L <value> Alias for --localize-symbol [139] --new-symbol-visibility=<value> [140] Visibility of symbols generated for binary input or added with --add-symbol unless otherwise specified. The default value is 'default' [141] -N <value> Alias for --strip-symbol [142] --only-keep-debug Produce a debug file as the output that only preserves contents of sections useful for debugging purposes [143] --only-section=section Remove all but <section> [144] --output-target=<value> Format of the output file [145] -O <value> Alias for --output-target [146] --pad-to=address Pad the output up to the load address <address>, using a value of zero or the value specified by the --gap-fill option. This option is only supported for ELF input and binary output [147] --prefix-alloc-sections=prefix [148] Add <prefix> to the start of every allocated section name [149] --prefix-symbols=prefix Add <prefix> to the start of every symbol name [150] --preserve-dates Preserve access and modification timestamps [151] -p Alias for --preserve-dates [152] --redefine-sym=old=new Change the name of a symbol old to new [153] --redefine-syms=filename [154] Reads a list of symbol pairs from <filename> and runs as if --redefine-sym=<old>=<new> is set for each one. <filename> contains two symbols per line separated with whitespace and may contain comments beginning with '#'. Leading and trailing whitespace is stripped from each line. May be repeated to read symbols from many files [155] --regex Permit regular expressions in name comparison [156] --remove-section=section [157] Remove <section> [158] --rename-section=old=new[,flag1,...] [159] Renames a section from old to new, optionally with specified flags. Flags supported for GNU compatibility: alloc, load, noload, readonly, exclude, debug, code, data, rom, share, contents, merge, strings, large [160] -R <value> Alias for --remove-section [161] --set-section-alignment=section=align [162] Set alignment for a given section [163] --set-section-flags=section=flag1[,flag2,...] [164] Set section flags for a given section. Flags supported for GNU compatibility: alloc, load, noload, readonly, exclude, debug, code, data, rom, share, contents, merge, strings, large [165] --set-section-type=section=type [166] Set the type of section <section> to the integer <type> [167] --set-start=addr Set the start address to <addr>. Overrides any previous --change-start or --adjust-start values [168] --shared-obj=filename Specify a static shared object file as input for building an mmELF file. [169] --split-dwo=dwo-file Equivalent to extract-dwo on the input file to <dwo-file>, then strip-dwo on the input file [170] --strip-all-gnu Compatible with GNU's --strip-all [171] --strip-all Remove non-allocated sections outside segments. .gnu.warning* and .ARM.attribute sections are not removed [172] --strip-debug Remove all debug sections [173] --strip-dwo Remove all DWARF .dwo sections from file [174] --strip-non-alloc Remove all non-allocated sections outside segments [175] --strip-sections Remove all section headers and all sections not in segments [176] --strip-symbol=symbol Strip <symbol> [177] --strip-symbols=filename [178] Reads a list of symbols from <filename> and removes them [179] --strip-unneeded-symbol=symbol [180] Remove symbol <symbol> if it is not needed by relocations [181] --strip-unneeded-symbols=filename [182] Reads a list of symbols from <filename> and removes them if they are not needed by relocations [183] --strip-unneeded Remove all symbols not needed by relocations [184] --subsystem=name[:version] [185] Set PE subsystem and version [186] -S Alias for --strip-all [187] --target=<value> Format of the input and output file [188] --update-section=name=file [189] Replace the contents of section <name> with contents from a file <file> [190] -U Alias for --disable-deterministic-archives [191] --version Print the version and exit. [192] -V Alias for --version [193] --weaken-symbol=symbol Mark <symbol> as weak [194] --weaken-symbols=filename [195] Reads a list of symbols from <filename> and marks them weak [196] --weaken Mark all global symbols as weak [197] --wildcard Allow wildcard syntax for symbol-related flags. Incompatible with --regex. Allows using '*' to match any number of characters, '?' to match any single character, '' to escape special characters, and '[]' to define character classes. Wildcards beginning with '!' will prevent a match, for example "-N '*' -N '!x'" will strip all symbols except for "x". [198] -W <value> Alias for --weaken-symbol [199] -w Alias for --wildcard [200] -X Alias for --discard-locals [201] -x Alias for --discard-all [202]Pass @FILE as argument to read options from FILE. [203]gmake[1]: Target 'secondary-outputs' not remade because of errors. [204]gmake: *** [makefile:155: all] Error 2 报错
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值